如何防止非专用模板实例化?
我有一个模板化的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
。
I have a templated class
(call it Foo
) which has several specializations. I would like the compilation to fail if someone tries to use an unspecialized version of Foo
.
Here is what I actually have:
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>
{ };
So that:
int main()
{
Foo<int> foo;
}
Works while:
int main()
{
Foo<char> foo;
}
Does not.
Obviously, the compiler chain only complains when the linking process takes place. But is there a way to make it complain before ?
I can use boost
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只是不要定义类:
为什么这有效?只是因为没有任何通用模板。声明了,是的,但没有定义。
Just don't define the class:
Why does this work? Simply because there isn't any generic template. Declared, yes, but not defined.
您可以简单地不定义基本情况:
You can simply not define the base case:
C++0x 的一个技巧(也可用于 C++03
static_assert
模拟,但错误消息并不一定比未定义主模板更好):断言仅在以下情况下触发 : code>Foo 使用主模板进行实例化。使用
static_assert( false, ... )
将始终触发断言。A trick for C++0x (also available with a C++03
static_assert
emulation, but the error message isn't necessarily better than leaving the primary template undefined):The assertion will only trigger when
Foo
is instantiated with the primary template. Usingstatic_assert( false, ... )
would trigger the assertion all the time.