C ++ decltype无法推断类型 [英] C++ decltype fails to deduce type
问题描述
在Visual Studio 2012中,decltype确实是个问题吗?还是应该很难使用?
Is decltype really buggy in Visual Studio 2012 or is it actually supposed to be this hard to use?
示例:
namespace ptl
{
struct Test
{
Test(float ){}
};
template<class T, class A0>
static T* static_constructor(void* p, A0 a0){return new(p) T(a0);}
template<class T>
T* MakeVS2012Happy(T*);
}
inline auto ExampleFxn() -> decltype(ptl::MakeVS2012Happy(&ptl::static_constructor<ptl::Test, float>))
{
return &ptl::static_constructor<ptl::Test, float>;
}
inline auto ExampleFxn2() -> decltype(&ptl::static_constructor<ptl::Test, float>)
{
return &ptl::static_constructor<ptl::Test, float>;
}
ExampleFxn会进行编译,因为我已使用无意义的函数将代码包装在decltype中
ExampleFxn compiles because I've wrapped the code in the decltype with that pointless function.
ExampleFxn2没有,VS2012发出非常有用的错误消息:
ExampleFxn2 does not, VS2012 spits out the extremely helpful error message:
错误C3555:错误的参数'decltype'
error C3555: incorrect argument to 'decltype'
有人知道是什么原因吗?我似乎经常需要与decltype对抗,以使其按预期工作……
Anyone know what causes this? I seem to constantly have to fight against decltype to get it to work as expected...
谢谢
推荐答案
& ptl :: static_constructor< ptl :: Test,float>
的类型可以由推导decltype()
。这看起来像MSVC ++中的错误。 clang和gcc都认为代码很好(假设添加了 #include< new>
)。
The type of &ptl::static_constructor<ptl::Test, float>
can be deduced by decltype()
. This looks like a bug in MSVC++. Both clang and gcc agree that the code is fine (assuming #include <new>
is added).
这篇关于C ++ decltype无法推断类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!