【发布时间】:2011-10-27 04:50:07
【问题描述】:
我有一个模板化的class(称之为Foo),它有几个专业。如果有人尝试使用非专业版本的Foo,我希望编译失败。
这是我实际拥有的:
template <typename Type>
class Foo
{
Foo() { cannot_instantiate_an_unspecialized_Foo(); }
// This method is NEVER defined to prevent linking.
// Its name was chosen to provide a clear explanation why the compilation failed.
void cannot_instantiate_an_unspecialized_Foo();
};
template <>
class Foo<int>
{ };
template <>
class Foo<double>
{ };
这样:
int main()
{
Foo<int> foo;
}
工作时间:
int main()
{
Foo<char> foo;
}
没有。
显然,编译器链仅在链接过程发生时才会抱怨。但是有没有办法让它之前抱怨?
我可以使用boost。
【问题讨论】:
标签: c++ templates boost compilation