2012-12-20 8 views
6

gcc/4.7을 사용하고 있으며 템플릿 함수 (또는 멤버 함수)에서 템플릿 템플릿 인수로 클래스를 인스턴스화해야합니다. 나는 코드가 실행되는 코드에서 표시된 두 줄을 주석으로 다음과 같은 오류템플릿 템플릿 코드가 작동하지 않습니다.

test.cpp: In function 'void setup(Pattern_Type&)': 
test.cpp:17:34: error: type/value mismatch at argument 1 in template parameter list for 'template<template<class> class C> struct A' 
test.cpp:17:34: error: expected a class template, got 'typename Pattern_Type::traits' 
test.cpp:17:37: error: invalid type in declaration before ';' token 
test.cpp:18:5: error: request for member 'b' in 'a', which is of non-class type 'int' 

을받을 수 있으므로 A A는 '주'가 아닌 '설정'에서 인스턴스화 할 수 있습니다. 나는 이것이 다른 사람들에게도 흥미로울 것이라고 생각하며, 코드가 작동하지 않는 이유를 이해하게되어 정말 기쁩니다. 코드는 다음과 같습니다.

struct PT { 
    template <typename T> 
    struct traits { 
    int c; 
    }; 
}; 

template <template <typename> class C> 
struct A { 
    typedef C<int> type; 
    type b; 
}; 

template <typename Pattern_Type> 
void setup(Pattern_Type &halo_exchange) { 
    A<typename Pattern_Type::traits> a; // Line 17: Comment this 
    a.b.c=10; // Comment this 
} 

int main() { 
    A<PT::traits> a; 

    a.b.c=10; 

    return 0; 
} 

의견이나 제안에 감사드립니다. 마우로는

+0

MSVC10에서 컴파일됩니다. –

답변

9

당신은 템플릿으로 Pattern_Type::traits을 표시해야합니다 : 그것은 템플릿 매개 변수 Pattern_Type에 의존하기 때문에

A<Pattern_Type::template traits> a; 

이 필요합니다.

traits은 형식이 아니기 때문에 typename을 사용하면 안됩니다.

+0

그것은 내가 시도하지 않은 조합입니다 : typename없이 템플릿. 아직도 '기본'에서 '템플릿'을 지정할 필요가없는 이유를 명확히 밝히지 않았습니다. 슈퍼 빠른 답변 주셔서 감사합니다! – user1919074

+1

@ user1919074 :'main'에서 템플릿 매개 변수에 의존하지 않아서 (즉'PT '의 내용은 이미 알려져 있지만'Pattern_Type'의 내용은 아무것도 될 수 없기 때문에 알 수 없기 때문입니다). 자세한 내용은 [위치 및 왜 "템플릿"및 "typename"키워드를 넣어야합니까?] (http://stackoverflow.com/questions/610245/where-and-why-do-i-have- to-put-the-template-and-typename- 키워드) – interjay

+0

맞습니다.이 사실을 알고 있었어야합니다 ... – user1919074

관련 문제