简体   繁体   English

使用(非类型)枚举参数定义内部类成员函数模板

[英]Defining an Inner class member function template with a (non type) enum argument

I'm having difficulty defining and specializing a member function update() of an inner class Outer<T1>::Inner that is templated on a non-type (enum) argument. 我很难定义和专门化一个内部类Outer<T1>::Inner的成员函数update() ,它是在非类型(枚举)参数上模板化的。

#include <cstdlib>

template<typename T1>
struct Outer
{
    struct Inner
    {
        enum Type{ A , B , C };

        template<Type T2>
        void update();
    };
};

// Definition
template<typename T1>
template<Outer<T1>::Inner::Type T2>
void Outer<T1>::Inner::update()
{
}

// Specialization
template<typename T1>
template<Outer<T1>::Inner::A >
void Outer<T1>::Inner::update()
{
}

int main()
{
    return EXIT_SUCCESS;
}

I'm getting the following error message in GCC 4.5.3 我在GCC 4.5.3中收到以下错误消息

prog.cpp:17:28: error: ‘Outer::Inner::Type’ is not a type
prog.cpp:18:6: error: prototype for ‘void Outer<T1>::Inner::update()’ does not match any in class ‘Outer<T1>::Inner’
prog.cpp:11:15: error: candidate is: template<class T1> template<Outer<T1>::Inner::Type T2> void Outer<T1>::Inner::update()
prog.cpp:24:28: error: ‘Outer::Inner::A’ is not a type
prog.cpp:25:6: error: prototype for ‘void Outer<T1>::Inner::update()’ does not match any in class ‘Outer<T1>::Inner’
prog.cpp:11:15: error: candidate is: template<class T1> template<Outer<T1>::Inner::Type T2> void Outer<T1>::Inner::update()

BTW, unlike GCC, Visual Studio 2008 is unable to compile the following BTW,与GCC不同,Visual Studio 2008无法编译以下内容

template<typename T1>
struct Outer
{
    struct Inner
    {
        enum Type{ A , B , C };

        template<Type T2>
        struct Deep;
    };
};

template<typename T1>
template<typename Outer<T1>::Inner::Type T2>
struct Outer<T1>::Inner::Deep
{
};

First of all, you're missing a typename before Outer<T1>::Inner::Type . 首先,你在Outer<T1>::Inner::Type之前缺少一个typename You have to have it, even in a template type list, because Type is a dependent type. 您必须拥有它,即使在template类型列表中也是如此,因为Type是依赖类型。

Secondly, your specialisation syntax is wrong (the type goes in <> after the function name before the parentheses, not in the template<> ), but even if it was correct, it would not be legal. 其次,你的专业化语法是错误的(类型在括号前的函数名之后的<>中,而不是在template<> ),但即使它是正确的,也不合法。 You have to specialise the outer template Outer before you can fully specialise update , according to an unfortunate rule regarding explicit template specialisation. 根据关于显式模板特化的不幸规则,您必须在完全专门化update之前专门化外部模板Outer

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 模板类型的成员枚举类作为函数参数 - Member enum class of template type as function argument c ++定义没有模板参数的成员类的类型 - c++ defining type of a member class without template argument 从非成员模板函数访问私有内部类类型 - Accessing private inner class type from non-member template function 成员函数的缩写参数,期望引入类型(枚举类) - Abbreviate argument to member function expecting introduced type (enum class) 非模板类T的模板成员函数(在T类型上) - Template member function (on type T) of non-template class T 带有非类型模板的模板类参数成员函数 - template class with Non-type template Parameter member function 为成员函数定义代理,该成员函数将成员函数指针作为模板参数 - Defining a proxy for a member function that takes a member function pointer as a template argument 将调用 class 的“this”或“type”隐式传递给非成员模板 function - Implicitly pass the "this" OR the "type" of the calling class to a non-member template function 通过定义基类来“多态”声明非专业模板类型的成员的唯一方法是吗? - Is the only way to “polymorphically” declare a member of a non-specialized template type, by defining a base class? 将指向数据成员的指针作为非类型模板参数传递时推断类型和 class - Inferring type and class when passing a pointer to data member as a non-type template argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM