如何创建模板属于特定类型的编译时断言?
我有一个模板函数,并希望在编译时确保它不会在特定类的子类型或超类型上实例化。
如果违反此规定,如何导致 C++ 编译器错误?
class base {
};
class derived : public base {
};
class lowest : public derived {
};
template <typename T>
bool isCorrect(const T& obj) {
typedef foo<T> D;
foo<T> *def = foo<T>::find();
return (def && def->getAnswer(object));
}
我希望 isCorrect
仅适用于类 衍生
,但不适用于 base
或 lowest
。请注意,可能存在许多其他最低类和要排除的一系列基类以及可接受的替代派生类。
C++ 中有没有办法限制模板仅适用于我明确指定的派生类?
I have a template function and wish to ensure at compile time that it is not instantiated on a subtype or supertype of a particular class.
How can I cause a C++ compiler error if this is violated?
class base {
};
class derived : public base {
};
class lowest : public derived {
};
template <typename T>
bool isCorrect(const T& obj) {
typedef foo<T> D;
foo<T> *def = foo<T>::find();
return (def && def->getAnswer(object));
}
I want isCorrect
to only be available for class derived
, but not base
or lowest
. Note there could be many other lowest classes and a string of base classes to be excluded as well as alternative derived classes that are acceptable.
Is there a way in C++ to limit the template to only apply to the derived classes I explicitly specify?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
类型特征,特别是
is_base_of
。请注意,这是 C++11 特定的,但您可以使用 Boost.TypeTraits 获得相同的行为。
Type traits, specifically
is_base_of
.Note that this is C++11 specific, but you can get the same behaviour with Boost.TypeTraits.
这是我所知道的一种技术。
首先,创建另一个模板类
policy_enforcer
。声明此类而不定义它,并为派生
提供它的特化也已定义:然后,在您希望的函数中锁定,包括表达式
sizeof(policy_enforcer)
。由于不完整类型上的sizeof
是一个编译错误,这将阻止代码编译。使用实时代码更新: 使用
base
, < a href="http://www.ideone.com/uOdLk" rel="nofollow">使用派生
,使用最低
。Here's one technique that I know of.
First, make another template class
policy_enforcer
. Declare this class without defining it, and also provide a specialization of it forderived
that is also defined:Then, within the function you wish to lock down, include the expression
sizeof(policy_enforcer<T>)
. Sincesizeof
on incomplete types is a compilation error, this will prevent the code from compiling.Updated with live code: using
base
, usingderived
, usinglowest
.您可以使用模板专门化。
您只能为您希望使用的类型实现
isCorrect
。对于其他类型,您可以实现虚拟方法,例如返回 false,或者根本不实现 isCorrect,在这种情况下,它将无法针对其他类型进行编译。
测试:
输出:
You can use template specialization.
You can implement only the
isCorrect
only for types that you want to be able to work with.For the other types you can implement dummy method, returning
false
for example, or not implementisCorrect
at all, in which case it will not compile for other types.Test:
Output: