Tin*_*gwa 4 c++ templates nested typename c++11
我在MSV2010中尝试以下内容
namespace statismo {
template<>
struct RepresenterTraits<itk::Image<itk::Vector<float, 3u>, 3u> > {
typedef itk::Image<itk::Vector<float, 3u>, 3u> VectorImageType;
typedef VectorImageType::Pointer DatasetPointerType;
typedef VectorImageType::Pointer DatasetConstPointerType;
typedef typename VectorImageType::PointType PointType;
typedef typename VectorImageType::PixelType ValueType;
};
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
错误C2899:typename不能在模板声明之外使用
我们将非常感谢您提供解决方案的帮助.
Tem*_*Rex 10
namespace statismo {
template<>
struct RepresenterTraits<itk::Image<itk::Vector<float, 3u>, 3u> > {
// bla
typedef typename VectorImageType::PointType PointType;
^^^^^^^^
typedef typename VectorImageType::PixelType ValueType;
^^^^^^^^
};
Run Code Online (Sandbox Code Playgroud)
您的typename关键字放在RepresenterTraits<T>for 的明确specalization中itk::Image<itk::Vector<float, 3u>, 3u>.但是,这是一个常规类,而不是类模板.这意味着它VectorImageType不是依赖名称,编译器知道它PixelType是嵌套类型.这就是为什么它不允许使用typename.请参阅此问答.
请注意,在C++ 11,该限制已经解除,并且使用的typename是允许的,但不是必需的非模板上下文.参见例如这个例子
#include <iostream>
template<class T>
struct V
{
typedef T type;
};
template<class T>
struct S
{
// typename required in C++98/C++11
typedef typename V<T>::type type;
};
template<>
struct S<int>
{
// typename not allowed in C++98, allowed in C++11
// accepted by g++/Clang in C++98 mode as well (not by MSVC2010)
typedef typename V<int>::type type;
};
struct R
{
// typename not allowed in C++98, allowed in C++11
// accepted by g++ in C++98 mode as well (not by Clang/MSVC2010)
typedef typename V<int>::type type;
};
int main()
{
}
Run Code Online (Sandbox Code Playgroud)
实例(只需使用g ++/clang和std = c ++ 98/std = c ++ 11命令行选项).