C++ 嵌套结构的 typedef(元类)编译错误

C++ nested structs' typedefs (metaclass) compilation error

我想创建嵌套的模板化结构 typedef 来创建一些带有元方法的元类,它可以有不同的参数。示例代码如下:

#include <iostream>
using namespace std;

template <int one, int two, int three>
struct vector_c{
    enum{
    v1 = one,
    v2 = two,
    v3 = three
    };

    template <typename vector>
    struct sum{
        typedef vector_c<
            one+vector::v1,
            two+vector::v2,
            three+vector::v3
        > type;
    };
};


int main() {
    vector_c<1,2,3>::sum<vector_c<3,2,1>>::type asdf;
    return 0;
}

这给了我以下错误:

prog.cpp:24: error: '::type' has not been declared prog.cpp:24: error: template argument 3 is invalid prog.cpp:24: error: template argument 1 is invalid

示例可在此处找到:

https://ideone.com/9BQ2FI

我怎样才能实现我的目标?可能吗?

这应该可以在 C++11 中工作,看起来你是用一个很老的编译器试过的。

vector_c<1,2,3>::sum<vector_c<3,2,1>>::type asdf;

在 C++11 之前,如果嵌套模板没有白色 space,就不可能有 >>。如果您添加 space:

它应该可以工作
vector_c<1,2,3>::sum<vector_c<3,2,1> >::type asdf;
//                                  ^