C++11 visual studio 2013 std::bind with vector返回空函数
此代码C++11 visual studio 2013 std::bind with vector返回空函数,c++11,msvc12,C++11,Msvc12,此代码 #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
#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 );
}
如果我为struct S
提供构造函数(取消注释第十行),它将打印四个ok
当我用GCC编译这段代码(没有构造函数)时,它打印了四个ok
Q:MS的编译器中是否存在缺陷,或者我做错了什么,而我只是对GCC感到“幸运”
另外,我的工作室是“Microsoft Visual studio Ultimate 2013”。版本:“12.0.21005.1 REL”它可以在我的MSVC 2013中打印所有内容……谢谢,@EdgarRokyan。什么是你工作室的完整版本?我用oneMSVC express 12.0.30723.00更新3更新了问题。另外,至少从我的角度来看,你的代码看起来非常有效:)@Edgarokyan,我将我的studio升级为更新5,并得到了预期的结果-全部
ok
。СПаббббббббббббббббббб107。什么是你工作室的完整版本?我用oneMSVC express 12.0.30723.00更新3更新了问题。另外,至少从我的角度来看,你的代码看起来非常有效:)@Edgarokyan,我将我的studio升级为更新5,并得到了预期的结果-全部ok
。СПаббббббббббббббб1073?
ok : 1
ok : 2
fail
ok : 42