c++ - Does std::function() check the type when compiling code in C++11 in VS2010? -
i using std::function()
in code c++11 , vs2010. here code.
function<string (string)> myfunc = 3; myfunc(string());
it obvious code incorrect ,for myfunc
object not initialized properly. myfunc
object should valued function<string (string)>
object other integer type. , when running code myfunc(string());
, std::function
object throws bad_function_call()
, gets error.
however,what confused me why c++11 not check type during compiling time? strange because lack of type check make programmers mess code incidentally, until running according error line.
so want ask: std::function()
check type when compiling code in c++11 in vs2010? if not,is there solution problem?
looks hack in ms's implementation of std::function
allow following:
function<string (string)> myfunc = null;
this piece of code find in ms's implementation of <functional>
header:
#if defined(_native_nullptr_supported) \ && !defined(_do_not_use_nullptr_in_stl) function(int) { // construct empty function wrapper null pointer this->_reset(); } #endif /* defined(_native_nullptr_supported) etc. */
so looks constructor enabled conditionally based on level of standard conformance user wants.
however, there no such constructor in c++11 standard library specification (see paragraph 20.8.11.2.1).
Comments
Post a Comment