클래스 템플릿 외부의 클래스 템플릿 템플릿 함수가 될 수 있습니까? 구문은 무엇입니까? 코드에 따라클래스 템플릿 외부의 클래스 템플릿 템플릿 함수가 될 수 있습니까?
준다 unable to match function definition to an existing declaration
편집
#include <iostream>
template <typename T>
struct Test
{
template <typename S>
void test(const S & t);
//this works
//template<> void test(const double & t) { std::cout << t << "D \n"; }
T member;
};
//this doesn't work
template <typename T>
template <>
void Test<T>::test(const double & t)
{
std::cout << t << "D \n";
}
int main()
{
Test<int> t;
t.test(7.0);
}
MSVC2010에서
답변에 제안 나는 조금 다르게 사용하기 때문에 내가 여기에, 오버로드를 사용할 수있는 방법입니다 :
#include <iostream>
template <typename T>
struct Test
{
template <typename S>
void test() { std::cout << "Any type \n"; }
template <>
void test<double>() { std::cout << "Double! \n"; }
T member;
};
int main()
{
Test<int> t1;
Test<int> t2;
t1.test<float>();
t2.test<double>();
}
과 나는 struct
외부의 이중화를 원합니다.
왜 이런 식으로 사용합니까? 실제 시나리오에서는 내가 좋아하는 사용되는 팩토리 클래스 구축 :
Factory<SomePolicy> f;
f.create<MyType>(arg1, arg2, ...)
을 내가 헤더 파일을 오염하지 않습니다 특정 유형에 대한 create
의 전문화가 필요합니다.
빈'template <>'은 비어 있지 않은'template'을 따라갈 수 없습니다. 그래서 다음과 같이 쓸 수 있습니다 :'template <> template void Test :: test (const S & t) {/*...*/}'이것은 당신이 원하는 것이 아닙니다. 그리고 클래스 선언 내에서는 전문화를 선언하거나 정의 할 수 없습니다. –
Constructor