문제

다음 함수 템플릿에서 두 번째 괄호 <>의 이유는 무엇입니까?

template<> void doh::operator()<>(int i)

이것은 들어 왔습니다 그래서 질문 이후에 괄호가 빠진 것으로 제안 된 곳 operator(), 그러나 설명을 찾을 수 없었습니다.

형식의 유형 전문화 (전체 전문화)라면 의미를 이해합니다.

template< typename A > struct AA {};
template<> struct AA<int> {};         // hope this is correct, specialize for int

그러나 기능 템플릿의 경우 :

template< typename A > void f( A );
template< typename A > void f( A* ); // overload of the above for pointers
template<> void f<int>(int);         // full specialization for int

이것은이 시나리온에 어디에 적합합니까? :

template<> void doh::operator()<>(bool b) {}

작동하는 것으로 보이며 경고/오류를 제공하지 않는 코드 (GCC 3.3.3 사용) :

#include <iostream>
using namespace std;

struct doh
{
    void operator()(bool b)
    {
        cout << "operator()(bool b)" << endl;
    }

    template< typename T > void operator()(T t)
    {
        cout << "template <typename T> void operator()(T t)" << endl;
    }
};
// note can't specialize inline, have to declare outside of the class body
template<> void doh::operator()(int i)
{
    cout << "template <> void operator()(int i)" << endl;
}
template<> void doh::operator()(bool b)
{
    cout << "template <> void operator()(bool b)" << endl;
}

int main()
{
    doh d;
    int i;
    bool b;
    d(b);
    d(i);
}

산출:

operator()(bool b)
template <> void operator()(int i)
도움이 되었습니까?

해결책

나는 그것을 찾아서 14.5.2/2에 의해 지정되어 있음을 발견했습니다.

로컬 클래스에는 멤버 템플릿이 없습니다. 액세스 제어 규칙 (11 항) 멤버 템플릿 이름에 적용됩니다. 소멸자는 회원 템플릿이되어서는 안됩니다. 주어진 이름과 유형의 정상 (비 테일 플레이트) 멤버 함수와 동일한 유형의 전문화를 생성하는 데 사용할 수있는 동일한 이름의 멤버 함수 템플릿을 클래스에서 선언 할 수 있습니다. 둘 다 존재하는 경우, 해당 이름과 유형의 사용은 명시 적 템플릿 인수 목록이 제공되지 않는 한 비 테일 플레이트 멤버를 나타냅니다.

그리고 예를 제공합니다.

template <class T> struct A {
    void f(int);
    template <class T2> void f(T2);
};

template <> void A<int>::f(int) { } // non-template member
template <> template <> void A<int>::f<>(int) { } // template member

int main()
{
    A<char> ac;
    ac.f(1); //non-template
    ac.f(’c’); //template
    ac.f<>(1); //template
}

표준 용어로 specialization 명시 적 전문화를 사용하여 쓰는 기능과 인스턴스화를 사용하여 생성 된 기능을 말하며,이 경우 생성 된 전문화와 관련이 있습니다. specialization 템플릿을 명시 적으로 전문으로 사용하여 만든 기능을 참조 할뿐만 아니라 종종 사용되는 템플릿을 참조하십시오.

결론 : GCC가 잘못됩니다. Comeau는 코드를 테스트 한 Comeau가 올바르게 가져 와서 진단을 발행합니다.

"ComeauTest.c", 16 행 : 오류 : "void doh::operator()(bool)" 명시 적으로 전문화 될 수있는 실체가 아닙니다 template<> void doh::operator()(bool i)

템플릿의 전문화에 대해 불평하지 않습니다. int (만 bool), 동일한 이름을 언급하지 않기 때문에 그리고 유형 : 전문화가 가질 수있는 기능 유형은 void(int), 이는 비 템플릿 멤버 함수의 함수 유형과 다릅니다. void(bool).

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top