Xeo*_*Xeo 15 c++ templates instantiation visual-studio-2010 c++11
更新如下.
以下是我在main.cpp中的完整代码:
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(){
}
Run Code Online (Sandbox Code Playgroud)
但是我在Visual Studio 2010中遇到以下错误,而g ++编译得很好:
src\main.cpp(9):错误C2146:语法错误:缺少';' 在标识符'type'之前
--src\main.cpp(10):参见类模板实例化'some_traits<T>'正在编译
src\main.cpp(9):错误C2868:'some_traits<T>::type':using声明的非法语法; 预期的合格名称
(我喜欢最后一个,总重量.)
我可以将其视为VC10中的错误,还是有充分的理由进行早期实例化?或者它是一个错误decltype,使编译器认为这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())>
{};
Run Code Online (Sandbox Code Playgroud)
但是编译器仍然试图在现场实例化/编译所有内容,引发这些错误:
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=
]
Run Code Online (Sandbox Code Playgroud)
请注意,模板参数为空.有任何想法吗?
这似乎是一个错误(如果没有设置特殊标志,如下所述)。以下是Oracle 网站上C++ 模板的摘录:
7.2.2
ISO C++ 标准允许开发人员编写模板类,其中所有成员对于给定的模板参数可能不合法。只要不实例化非法成员,程序仍然是良好的。ISO C++ 标准库使用这种技术。但是, -template=wholeclass选项会实例化所有成员,因此在使用有问题的模板参数实例化时不能与此类模板类一起使用。
| 归档时间: |
|
| 查看次数: |
2974 次 |
| 最近记录: |