[英]Inconsistent brace-or-equal initialization behavior between MSVC and Clang
我有以下代码,我在Visual Studio 2013和Clang上编译:
#include <memory>
template<typename T>
class foo
{
public:
typedef void (T::*CallbackFn)();
foo(T* mem, CallbackFn cb) : m_member(mem), m_cb(cb) {}
private:
T* m_member;
CallbackFn m_cb;
};
class another
{
private:
void callback() {}
public:
std::unique_ptr<foo<another>> f{new foo<another>(this, &another::callback)};
};
int main() {}
( Coliru的现场样本 )
在clang和GCC上编译时,此代码工作正常。 但是在VS2013上它失败了:
main.cpp(22): error C2276: '&' : illegal operation on bound member function expression
main.cpp(22): error C2664: 'foo<another>::foo(const foo<another> &)' : cannot convert argument 1 from 'another *const ' to 'const foo<another> &'
Reason: cannot convert from 'another *const ' to 'const foo<another>'
No constructor could take the source type, or constructor overload resolution was ambiguous
出于某种原因,VS编译器试图调用foo
的拷贝构造函数,这根本没有意义。 它完全忽略了构造函数的第二个参数。
有趣的是,如果我将foo
的构造函数调用更改为大括号,如下所示:
std::unique_ptr<foo<another>> f{new foo<another>{this, &another::callback}};
它在MSVC上运行得很好。 谁能解释这种行为? 一种方式比另一种更正确吗? 这只是另一个MSVC错误还是由于某些不受支持的C ++ 11功能?
谁能解释这种行为?
一旦编译器遇到第一个错误,其余的只是垃圾。 忽略它。 (我通常只看到出现的第一个编译器错误,请参阅此处的VC ++)
一种方式比另一种更正确吗?
在这种情况下,两者都完全相同。 MSVC根本无法解析&another::callback
,然后忽略它以进一步分析该行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.