2012-10-23 2 views
11

나는 ... 그래서 같은 cpp에있는 전문 기능을 정의 할 수 있습니다cpp에서 템플릿 전문화를 정의 하시겠습니까?

// 헤더

template<typename T> 
void func(T){} 

template<> 
void func<int>(int); 

// cpp에 내가 전문 클래스의 메소드를 정의 할 수있는 방법

template<> 
void func<int>(int) 
{} 

Cpp에? 그래서처럼 ... (작동하지 않는, 나는 error C2910: 'A<int>::func' : cannot be explicitly specialized 수)

// 헤더

template<typename T> 
struct A 
{ 
    static void func(T){} 
}; 

template<> 
struct A<int> 
{ 
    static void func(int); 
}; 

// cpp에

template<> 
void A<int>::func(int) 
{} 
+0

당신은 체크 아웃이 되었습니까 : http://stackoverflow.com/questions/115703/storing-c-template-function-definitions-in-a-cpp-file – Reunanen

+0

@Pukku 그 방법의 문제 전문의 비 템플릿 (non-templated) 클래스. 독립 실행 형 함수의 전문화와 실제로 동일합니다 (위의 예제 에서처럼). 나는 cpp의 특수한 _class_에 메소드를 정의하는 방법에 대해 묻고있다. – David

답변

4

사용하여 .cpp 파일에서 구문 다음

void A<int>::func(int) 
{ 
} 

이것은 Visual C++의 일부 기능입니다.

은 자세한 내용 MSDN C2910 error description를 참조하십시오

이 오류는 또한 2003의 Visual Studio .NET에서 이루어졌다 컴파일러 준수 작업의 결과로 생성됩니다 :. 코드가 Visual Studio .NET 2003 및 Visual Studio .NET 버전의 Visual C++에서 유효한 경우 템플릿 <>을 제거하십시오.

+3

이것은 Visual Studio의 컴파일러 종속 기능입니다. 질문 http://stackoverflow.com/questions/3749099/why-should-the-implementation-and-the-declaration-of-a-template-class-be-in-the 및 http://stackoverflow.com/을 참조하십시오. 질문/495021/why-can-templates-only-be-in-the-header-file – 8bitwide

관련 문제