a surprising value about lambda and cin.get

89 Views Asked by At

environment: vs2013 rc5 / vs2017;
project: win32 console application;

representation:compile and run a little while,then interrupt and watch variable "task_";
if "add_task(&Test::print, &t, str, 10)" in func main, the "task_" is correct value;

but if "add_task(&Test::print, &t, str, 10)" in func mytest, the "task_" is wrong value; and if replace std::cin.get() with while(1){}, it turns right;


#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <functional>

using task_t = std::function<void()>;

class Test
{
public:
    void print(const std::string& str, int i)
    {
    std::cout << "Test: " << str << ", i: " << i << std::endl;
    }
};

template<typename Function, typename Self, typename... Args>
void add_task(const Function& func, Self* self, Args... args)
{
    task_t task = [&func, &self, args...]{ return (*self.*func)(args...); };    
    task_ = task;
}

Test t;
std::string str = "Hello world";

task_t task_ = nullptr;

void mytest()
{
    add_task(&Test::print, &t, str, 10);
}

int main()
{
    mytest();
    std::cin.get();
    return 0;
}
2

There are 2 best solutions below

0
On

thx your answer. in affect, i run debug version and close all optimize, it happen as befor. in wrong case, i insert task_() just befor std::cin.get(), it will crash:

    cccccccc()  Unknown
    [Frames below may be incorrect and/or missing]  
>   tp_test.exe!add_task::__l3::<lambda>() Line 21  C++
    tp_test.exe!std::_Callable_obj<void <lambda>(void),0>::_ApplyX<void>() Line 284 C++
    tp_test.exe!std::_Func_impl<std::_Callable_obj<void <lambda>(void),0>,std::allocator<std::_Func_class<void> >,void>::_Do_call() Line 229    C++
    tp_test.exe!std::_Func_class<void>::operator()() Line 315   C++
    tp_test.exe!main() Line 39  C++
    tp_test.exe!__tmainCRTStartup() Line 626    C
    tp_test.exe!mainCRTStartup() Line 466   C
    kernel32.dll!765c8744() Unknown
    ntdll.dll!__RtlUserThreadStart()    Unknown
    ntdll.dll!__RtlUserThreadStart@8()  Unknown

so i debug, breakpoint at task_(), press F11 and disassembly, crash at file "functional":

    _Ret operator()(_Types... _Args) const
        {   // call through stored object
00BE36A0  push        ebp  
00BE36A1  mov         ebp,esp  
00BE36A3  sub         esp,0CCh  
00BE36A9  push        ebx  
00BE36AA  push        esi  
00BE36AB  push        edi  
00BE36AC  push        ecx  
00BE36AD  lea         edi,[ebp-0CCh]  
00BE36B3  mov         ecx,33h  
00BE36B8  mov         eax,0CCCCCCCCh  
00BE36BD  rep stos    dword ptr es:[edi]  
00BE36BF  pop         ecx     <==========crash
00BE36C0  mov         dword ptr [this],ecx  
0
On

In C++, the compiler is not required to do exactly what you think it would if it executes your code line by line. It may optimize according to the "as-if" rule, meaning that it can do something else that will go faster if the observable behavior is the same.

Observable behavior does not include running the code in a debugger.

The variable task_ never has any consequence for any observable behavior, so a conforming compiler may optimize it out entirely. The function add_task doesn't affect any observable behavior, so a conforming compiler may optimize it out entirely.

(I'm not sure if it would actually do so here, but only because std::function may have to make a dynamic allocation, and when that happens it becomes harder for the compiler to reason about observable side-effects.)

Also, since my_test does not have any consequences for anything touched by std::cin.get(), the compiler is free to reorder these two statements if it think it might go faster.

You should do things like, run the function in test_ variable, if you want to force the optimizer to do things in a particular order, or commit to particular memory contents at a particular time.