使用(非类型)枚举参数定义一个内部类成员函数模板 [英] Defining an Inner class member function template with a (non type) enum argument

查看:205
本文介绍了使用(非类型)枚举参数定义一个内部类成员函数模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法定义和专门处理内部类外部< T1> :: Inner 的成员函数 update()

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.

#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;
}



我在GCC 4.5.3中收到以下错误消息

I'm getting the following error message in 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,与GCC不同,Visual Studio 2008无法编译以下


BTW, unlike GCC, Visual Studio 2008 is unable to compile the following

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
{
};


推荐答案

首先, c $ c> typename 之前外部< T1> :: Inner :: Type 。您必须拥有它,即使在模板类型列表中,因为类型是依赖类型。

First of all, you're missing a typename before Outer<T1>::Inner::Type. You have to have it, even in a template type list, because Type is a dependent 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. You have to specialise the outer template Outer before you can fully specialise update, according to an unfortunate rule regarding explicit template specialisation.

这篇关于使用(非类型)枚举参数定义一个内部类成员函数模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆