내가 클래스 정의/선언 내에서 공용 멤버 함수 템플릿을 전문으로하려고 :클래스 내에서 멤버 함수 템플릿을 특수화 할 수없는 이유는 무엇입니까?
#include <iostream>
class surfaceMesh
{
public:
// Uncomment for Version 0 and 1
class AREA_AVERAGE {};
class ANGLE_AVERAGE {};
template<class Average>
void vertexNormals() {}
// Uncomment: Version 0
//template<>
//void vertexNormals<AREA_AVERAGE>()
//{
//std::cout << "AREA_AVERAGE" << std::endl;
//}
//template<>
//void vertexNormals<ANGLE_AVERAGE>()
//{
//std::cout << "ANGLE_AVERAGE" << std::endl;
//}
};
// Uncommend for version 1
template<>
void surfaceMesh::vertexNormals<surfaceMesh::AREA_AVERAGE>()
{
std::cout << "AREA_AVERAGE" << std::endl;
};
template<>
void surfaceMesh::vertexNormals<surfaceMesh::ANGLE_AVERAGE>()
{
std::cout << "ANGLE_AVERAGE" << std::endl;
};
int main()
{
surfaceMesh m;
m.vertexNormals<surfaceMesh::AREA_AVERAGE>();
m.vertexNormals<surfaceMesh::ANGLE_AVERAGE>();
return 0;
}
버전 0의 경우, 오류는 다음과 같습니다
main.cpp:19: error: template-id ‘vertexNormals<mesh::AREA_AVERAGE>’ in declaration of primary template
main.cpp:24: error: explicit specialization in non-namespace scope ‘class mesh’
main.cpp:25: error: template-id ‘vertexNormals<mesh::ANGLE_AVERAGE>’ in declaration of primary template
main.cpp:25: error: ‘void mesh::vertexNormals()’ cannot be overloaded
main.cpp:19: error: with ‘void mesh::vertexNormals()’
버전 1 컴파일 및 실행됩니다. 물론, 보통 클래스 선언과 정의를 분리하려고합니다. 그러나 이것이 실제로 일어나는 이유를 알고 싶습니다.
또한 인터페이스를 특수화하는 좋은 방법입니까? 다른 옵션은 AREA_AVERAGE 또는 ANGLE_AVERAGE의 객체를 가져 오기 위해 vertexNormals 함수를 오버로드하는 것입니다. 그러나이 함수는 어떤 유형의 함수를 사용할 것인지를 알려주는 유형이며 인스턴스화하지 않아야하므로 템플릿을 사용하면 ' 올바른 선택처럼.
기능 템플릿을 부분적으로 특수화 할 수 없습니다 (클래스 외부에있는 것은 전공 분야입니다). –
놀랍게도 Visual Studio 2010의 버전 0은 – weidi
@ n.m에서 컴파일됩니다. 고마워. 고마워. – tmaric