为什么以下程序会出现“不是类、命名空间或枚举”的错误?

4
在下面的程序中,我希望it::type是double类型。但实际上,我得到了编译器错误提示:it不是类、命名空间或枚举。根据其他stackoverflow类似问题的答案,如果你没有使用参数实例化模板,就会出现这种错误。我认为我做得正确。请问,以下代码可能存在什么错误?
#include <iostream>
#include <tuple>

using namespace std;

template <typename ...Ts>
struct list {};

template <int I, typename T, typename ...Ts>
struct S {
        using type = typename S<I-1, Ts...>::type;
};

template <typename T, typename ...Ts>
struct S<0, T, Ts...> {
  using type = T;
};

template <int I, typename ...Ts>
S<I, Ts...> ith(list<Ts...>) {
        return S<I, Ts...>{};
}

int main() {
        auto l = list<const char *, void *, double>{};
        S<2, const char*, void *, double> it = ith<2>(l);
        it::type a = 1; // This line seems to cause the issue, 
                        // if removed the program compiles fine.
        return 0;
}

Error:
p.cpp:32:2: error: 'it' is not a class, namespace, or enumeration
        it::type a = 1;
        ^
p.cpp:31:36: note: 'it' declared here
        S<2, const char*, void *, double> it = ith<2>(l);
                                          ^
1 error generated.

请将完整的错误信息复制并粘贴到您的问题中。同时,请在代码行上添加注释以说明错误发生的位置。 - Some programmer dude
@Someprogrammerdude 谢谢你的建议。已完成。 - Abhishek Kumar
你能不能只写 decltype(it)::type a = 1; - Wyck
1个回答

4
您不能通过变量访问类型别名。
请改用以下方法:
decltype(it)::type a = 1;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接