更新如下。
template<class T>
struct other_traits;
template<class T>
struct some_traits{
typedef decltype(&T::operator()) Fty;
typedef typename other_traits<Fty>::type type;
};
int main(){
}
但是,当使用Visual Studio 2010编译时,我会遇到以下错误,而g++可以正常编译:
src\main.cpp(9): error C2146: 语法错误:在标识符“type”之前缺少“;”
--src\main.cpp(10) : 参见正在编译的类模板实例化“some_traits<T>
”
src\main.cpp(9): error C2868: 'some_traits<T>::type
' : 使用声明的语法非法;应为限定名
(我喜欢最后一个,完全不知所云。)
我能认为这是VC10的一个bug吗?还是有早期实例化的充分理由?或者是decltype
的一个bug使得编译器认为Fty
不是依赖名称?
更新:我尝试通过继承一个基类来欺骗编译器,让它认为Fty
是一个依赖名称:
template<class T>
struct other_traits;
template<class R, class C>
struct other_traits<R (C::*)()>{
typedef R type;
};
template<class Fty>
struct base_traits{
typedef typename other_traits<Fty>::type type;
};
template<class T>
struct some_traits
: public base_traits<decltype(&T::operator())>
{};
但编译器仍试图立即实例化/编译所有内容,并输出这些错误:
src\main.cpp(13): error C2039: 'type' : is not a member of 'other_traits<T>'
with
[
T=
]
src\main.cpp(19) : see reference to class template instantiation 'base_traits<Fty>' being compiled
with
[
Fty=
]
src\main.cpp(19) : see reference to class template instantiation 'some_traits<T>' being compiled
src\main.cpp(13): error C2146: syntax error : missing ';' before identifier 'type'
src\main.cpp(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
src\main.cpp(13): error C2602: 'base_traits<Fty>::type' is not a member of a base class of 'base_traits<Fty>'
with
[
Fty=
]
src\main.cpp(13) : see declaration of 'base_traits<Fty>::type'
with
[
Fty=
]
src\main.cpp(13): error C2868: 'base_traits<Fty>::type' : illegal syntax for using-declaration; expected qualified-name
with
[
Fty=
]
请注意,模板参数是空的。有什么想法吗?
typedef typename other_traits<typename Fty>::type type;
(只是一次盲猜)。 - Šimon TóthFty
,other_traits<Fty>
和other_traits<Fty>::type
都是类型。看起来 VC++ 在实例化之前进行了一些错误检查(在某种程度上是允许的),但可能因为使用了 "decltype" 而感到困惑。 - user180247decltype(...)
替换为简单的T
。 - Xeodecltype(...)
作为模板参数传递给基类,那么这个问题就可以解决。我会尽快尝试的。 - Xeo