This code
#include <functional>
#include <vector>
#include <iostream>
using std::cout;
using std::endl;
struct S {
std::function< void() > func;
//S( std::function< void() > && f ) : func( f ) {}
};
void foo( const S & s ) {
if ( s.func ) {
cout << "ok : ";
s.func();
}
else {
cout << "fail" << endl;
}
}
void bar_i( int i ) { cout << i << endl; }
void bar( std::vector< int > i ) { cout << i[ 0 ] << endl; }
int main() {
foo( S{ std::bind( bar_i, 1 ) } );
S s_i{ std::bind( bar_i, 2 ) };
foo( s_i );
std::vector< int > one = { 42 };
foo( S{ std::bind( bar, one ) } );
S s{ std::bind( bar, one ) };
foo( s );
}
compiles fine by MSVC 2013, but when I run the program it prints
ok : 1
ok : 2
fail
ok : 42
If I give the constructor for struct S
(uncomment the tenth line) it prints four ok
.
When I compiled this code (without constructor) by GCC, it printed four ok
Q: is there a bug in MS's compiler or I'm doing something wrong and I'm just "lucky" with GCC ?
P.S. My studio is "Microsoft Visual Studio Ultimate 2013". Version: "12.0.21005.1 REL"