EDIT: added the compiler errors at the end. First I'll say I have both Visual Studio Express 2013 and CodeBlocks (Mingw w64) set up and running. But I am having a problem with some code compiling with one compiler but not the other and vis-versa.
Consider this code:
void foo(std::string& s){
std::cout << "in foo function now" << std::endl;
s = "the new string.";
}
int main(){
std::string s = "the original string";
std::thread t1(foo, std::ref(s));
t1.join();
if (!s.size()) std::cout << "s is empty" << std::endl;
else std::cout << s << std::endl;
std::cin.get();
return 0;
}
It compiles and runs fine on both GCC and VS.
BUT if I decide to change std::ref(s)
with std::move(s)
it won't compile with GCC anymore but it will with VS.
To fix it I have to add a '&' so void foo(std::string& s)
becomes void foo(std::string&& s)
, THEN it will compile with gcc but it won't with VS anymore!
My guess would be that the VS compiler is quiet forgiving and is not following the standard to the letter but I haven't withness such a behavior with std::function and std::bind even though the arguments are passed the same way I believe.
At any rate I would very much appreciate some help in understanding what is going on.
EDIT: The errors: The GCC one:
C:/mingw-w64/x86_64-5.1.0-posix-seh-rt_v4-rev0/mingw64/x86_64-w64-mingw32/include/c++/functional:1505:61: error: no type named 'type' in 'class std::result_of<void (*(std::__cxx11::basic_string<char>))(std::__cxx11::basic_string<char>&)>'
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
C:/mingw-w64/x86_64-5.1.0-posix-seh-rt_v4-rev0/mingw64/x86_64-w64-mingw32/include/c++/functional:1526:9: error: no type named 'type' in 'class std::result_of<void (*(std::__cxx11::basic_string<char>))(std::__cxx11::basic_string<char>&)>'
_M_invoke(_Index_tuple<_Indices...>)
The VS one:
Error 1 error C2664: 'void (std::string &&)' : cannot convert argument 1 from 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>' to 'std::string &&' c:\program files (x86)\microsoft visual studio 12.0\vc\include\functional 1149 1 Tests