正在关注 this question
我得到了一个函数模板
template <typename T1, typename T2, typename IT1, typename IT2>
MethodResult<T1, T2>& method (IT1 it1begin, IT1 it1end, IT2 it2begin, IT2 it2end);
具有以下继承方案
class A
class B : A
class C : A
我想要执行的功能
IT1
和 IT2
是迭代器,分别取消对 T1
和 T2
的引用。
为此我试过了
static_assert(std::is_base_of< A,
typename list<T1>::iterator::value_type >::value,
"wrong type");
它编译得很好,但是它不会像我调用时那样强制执行任何内容
method<B, C>("not an", "iterator", 3, 2);
编译器不会产生错误。
请注意,我先试过了
static_assert(std::is_base_of<A,
typename std::iterator_traits<T1>::value_type,
"wrong type");
在我读完this question之后但编译器会产生。
wrong number of template arguments (3, should be 2)
关于如何实现这种类型检查有什么想法吗?
最佳答案
这个问题含糊不清,有几个问题。但我想我明白了它的要点。您可能正在寻找的static_assert
;
static_assert(std::is_base_of<A,
typename std::iterator_traits<IT1>::value_type>::value,
"wrong type");
您遗漏了一些括号和 value
等。我已将其格式化为类似于您的原始帖子。
关于c++ - 强制模板函数参数是特定类型的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24800253/