在VS2010中编译c++ 11中的代码时,std::function()是否检查类型
Does std::function() check the type when compiling code in C++11 in VS2010?
我在c++ 11和VS2010的代码中使用std::function()
。这是我的代码。
function<string (string)> myfunc = 3;
myfunc(string());
很明显代码是不正确的,因为myfunc
对象没有正确初始化。myfunc
对象应该用function<string (string)>
对象赋值,而不是整数类型。当运行到代码myfunc(string());
时,std::function
对象抛出bad_function_call()
并获得错误。
然而,让我困惑的是为什么c++ 11在编译时不检查类型?这很奇怪,因为缺乏类型检查会使程序员很容易偶然地把代码弄乱,直到运行到相应的错误行。
所以我想问:std::function()
在VS2010中编译c++ 11代码时检查类型吗?如果没有,这个问题有什么解决办法吗?
看起来这是MS的std::function
实现中的一个hack,允许以下内容:
function<string (string)> myfunc = NULL;
这是我可以在MS的<functional>
头的实现中找到的代码:
#if defined(_NATIVE_NULLPTR_SUPPORTED)
&& !defined(_DO_NOT_USE_NULLPTR_IN_STL)
function(int)
{ // construct empty function wrapper from null pointer
this->_Reset();
}
#endif /* defined(_NATIVE_NULLPTR_SUPPORTED) etc. */
因此,看起来这个构造函数是根据用户想要的标准一致性级别有条件地启用的。
但是,在c++ 11标准库规范中没有这样的构造函数(参见第20.8.11.2.1段)。
相关文章:
- 是否允许类类型的 std::function 成员变量(不完整类型)?
- 如何检查 log2 'inbuilt c++ function'是否返回 '-inf'?
- 是否可以列出所有可以通过".调用()'?中的错误。呼叫( "function" )
- 是否有任何 DirectX 11 (HLSL 5.0) 等效于 DirectX 9 纹理"string function"语法?
- 使用此"shortcutting function"是否是一种好的做法?
- 是否可以定义"template function pointer"的类型?
- 如何正确检查 std::function 在 C++11 中是否为空
- 是否存在GCC错误:默认std :: function
- C++模板是否能够从父类"forward any class function"?
- boost::function 和 std::tr1::function 之间是否有重要的区别需要了解
- 检查返回的 std::function 是否在 C++11 中"valid"
- 移动后是否应该 std::function::运算符布尔值返回 false
- 将 C++11 std::function 传递给采用 boost::function 的遗留函数是否安全?
- 是否使用快速委托(等)背后的想法来优化std::function
- 是否允许“std::function”移动其参数
- parallel_fo(Inter-TBB)上是否存在与我们在std::function上看到的开销类似的开销
- 在标准下调用 std::function<void(Args...)> 是否违法?
- 在VS2010中编译c++ 11中的代码时,std::function()是否检查类型
- 下面"calling function"的术语,它们是否是指调用正在初始化其参数的函数的函数?
- 函数中是否有任何内存泄漏,如function(new Foo())