C++ template 에서 일부 인자만 특수화 할때 클래스 전체를 특수화 하지 않고 메소드만 특수화 할 수 있나요?

조회수 1358회

C++ template specialization 을 테스트 중인데 specialization 2에서 에러가 나네요

// Specialization 2
template<class T> MyClassB<T,3>::MyClassB(){ cout<<"3"<<endl; }

일부 인자만 특수화 할때 클래스 전체를 특수화 하지 않고 메소드만 특수화 할 수 없는 건가요?

  • clang++ 703.0.3 으로 옵션 없이 컴파일 했습니다.
  • 전체 코드와 에러는 아래와 같습니다.
// TEMPLATE
template<class T, int I>
class MyClassB {
public:
  MyClassB(){ cout<<"DEFAULT"<<endl; }
};

// Specialization 1
template<class T>
class MyClassB<T,2> {
public:
  MyClassB();
};
template<class T> MyClassB<T,2>::MyClassB(){ cout<<"2"<<endl; }

// Specialization 2
template<class T> MyClassB<T,3>::MyClassB(){ cout<<"3"<<endl; }

// TEST
int main(){
  MyClassB<int, 1> a1;
  MyClassB<int, 2> a2;
  MyClassB<int, 3> a3;
}
  • ERROR
template2.cxx:20:34: error: nested name specifier 'MyClassB<T, 3>::' for declaration does not refer into a class, class template or
      class template partial specialization
template<class T> MyClassB<T,3>::MyClassB(){ cout<<"3"<<endl; }
  • (•́ ✖ •̀)
    알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)