[英]How can I use a nested type belonging to a templated class in another template function in C++?
I'm setting up a function that initializes tuples based on a tuple type and a functor struct For
that has a size_t template argument INDEX
to retain the compile-time index. 我正在设置一个函数,它基于元组类型和函子结构来初始化元组。 For
它有一个size_t模板参数INDEX
来保留编译时索引。 This functor may also depend on other template arguments T...
. 这个仿函数也可能依赖于其他模板参数T...
Because of this the functors exist within other structures ( TClass
in this example) that hold these template arguments. 因此, TClass
函数存在于TClass
这些模板参数的其他结构(本示例中为TClass
)中。
The initialization function (called Bar
here) has a template<std::size_t> class
template argument to ensure that the used class actually can store the index. 初始化函数(此处称为Bar
)具有template<std::size_t> class
模板参数,以确保所使用的类实际上可以存储索引。
While the design I've come up with works fine when I call it from a non-template function, it does not compile if the template T2
of a function does determine the template parameter of the wrapper TClass
. 虽然当我从非模板函数调用它时,我提出的设计工作正常,但如果函数的模板T2
确定包装器TClass
的模板参数,则它不会编译。
Here is the definition of the functor For
wrapped inside TClass
: 这是functor的定义For
裹在TClass
:
#include <cstdlib>
template <typename T> struct TClass {
template<std::size_t INDEX> struct For {
void operator()() {}
};
};
And here are the function calls i want to use: 以下是我想要使用的函数调用:
template <template<std::size_t> class FOR> void bar() {
//...
}
template <typename T> void foo() {
bar<TClass<T>::For>(); //Does not compile
}
int main() {
bar<TClass<int>::For>(); //Works
foo<int>();
return 0;
}
The compiler output for the faulty foo
-call is: 故障foo
-call的编译器输出是:
error: dependent-name ‘TClass<T>::For’ is parsed as a non-type, but instantiation yields a type
Bar<TClass<T>::For>(); //Does not compile
I know that dependent type names usually have to be preceded by a typename
but this is also not necessary for the first bar
-call. 我知道依赖类型名称通常必须以typename
开头,但这对于第一个bar
-call也不是必需的。 I assumed it was because the template argument can only be interpreted as a type. 我假设这是因为模板参数只能被解释为一种类型。 So I thought that maybe typename
would result in correct compilation but if I change foo
to 所以我认为也许typename
会导致正确的编译,但如果我改变foo
template <typename T> void foo() {
bar<typename TClass<T>::For>(); //Does not compile
}
I get: 我明白了:
error: ‘typename TClass<int>::For’ names ‘template<long unsigned int INDEX> struct TClass<int>::For’, which is not a type
Bar<typename TClass<T>::For>(); //Does not compile
I've also come up with a design where the ()
-operator of TClass
depends on the template INDEX
which also works fine because it is not necessary to use nested types anymore. 我还想出了一个设计,其中TClass
的()
- 操作TClass
依赖于模板INDEX
,它也可以正常工作,因为它不再需要使用嵌套类型。 It looks like this: 它看起来像这样:
#include <cstdlib>
template <typename T> struct TClass {
template<std::size_t INDEX> void operator()() {}
};
template <typename FOR> void bar() {
//...
}
template <typename T> void foo() {
bar<TClass<T>>(); //Does compile
}
Apparently it is not possible to use dependent type names in functions where the template of the type is determined by the function's template parameters, but why? 显然,在函数模板参数确定类型模板的函数中,不可能使用依赖类型名称,但为什么呢? And how do I implement this correctly? 我该如何正确实现? To make writing future type checks with type traits easier I would prefer it if I can use a functor. 为了使用类型特征更容易编写未来的类型检查我更喜欢它,如果我可以使用仿函数。
The compiler cannot know that TClass<T>::For
refers to a template at the first stage of template instantiation. 编译器无法知道TClass<T>::For
在模板实例化的第一阶段引用模板。 It needs a bit of help with template
keyword. 它需要一些关于template
关键字的帮助。 Fix: 固定:
template <typename T> void foo() {
bar<TClass<T>::template For>();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.