簡體   English   中英

使用(非類型)枚舉參數定義內部類成員函數模板

[英]Defining an Inner class member function template with 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;
}

我在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無法編譯以下內容

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

首先,你在Outer<T1>::Inner::Type之前缺少一個typename 您必須擁有它,即使在template類型列表中也是如此,因為Type是依賴類型。

其次,你的專業化語法是錯誤的(類型在括號前的函數名之后的<>中,而不是在template<> ),但即使它是正確的,也不合法。 根據關於顯式模板特化的不幸規則,您必須在完全專門化update之前專門化外部模板Outer

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM