简体   繁体   English

如何在C ++中的另一个模板函数中使用属于模板化类的嵌套类型?

[英]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.

相关问题 在C ++中使用模板化类的嵌套类作为模板模板参数 - Use nested class of templated class as template template parameter in C++ 如何在非模板化的类中使用模板特化? C ++ - How can i use template specialization within a non-templated class? c++ 如何在另一个函数中使用类对象? C++ - How can I use a class object in another function? c++ 如何为模板化嵌套类类型的函数声明参数? - How do I declare an argument for a function of a templated nested class type? C ++显式模板化函数实例化,用于不同类型的模板化类 - C++ Explicit templated function instantiation for templated class of different type 如何使用decltype作为模板化类的返回类型的模板参数? - How can I use decltype as the template parameter for a return type of a templated class? 如何模拟一个带有模板化args的函数,并在c ++中对它们应用模板化函数? - How do I template a function that takes templated args and applies a templated function on them in c++? 如何为模板化类型专门化/重载模板函数 - How can I specialize/overload a template function for a templated type 在 C++ 中,如何在另一个嵌套类中使用嵌套类类型(两个嵌套类在同一个外部类下) - In C++, how can I use a nested class type in an other nested class (the two nested classes are under the same outer class) C ++-在具有非类型模板参数的模板化类上专门化函数模板 - C++ - specialize function template on a templated class with a non type template parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM