【发布时间】:2022-06-22 17:58:26
【问题描述】:
我有一组大致定义如下的类:
template <typename U>
class Iterable {
// More code
};
class Container : public Iterable<Element> {
// More code
};
class Tuple : public Container {
// More code
};
class List {
public:
template <typename I, typename T>
requires std::is_base_of_v<Iterable<T>,I>
explicit List(const I& i) {
for (const T& e : i) elems_.emplace_back(e,true);
}
// More code
};
试图从Tuple 创建一个List 为
Tuple t1(1,2,3);
List l1(t1);
给出以下编译信息
/home/felix/git/libraries/cpp_script/tests/test_list.cpp:96:15: error: no matching function for call to ‘cs::List::List(cs::Tuple&)’
96 | List l1(t1);
| ^
In file included from /home/felix/git/libraries/cpp_script/tests/test_list.cpp:3:
/home/felix/git/libraries/cpp_script/include/list.hpp:72:10: note: candidate: ‘template<class I, class T> requires is_base_of_v<cs::Iterable<T>, I> cs::List::List(const I&)’
72 | explicit List(const I& i) {
| ^~~~
/home/felix/git/libraries/cpp_script/include/list.hpp:72:10: note: template argument deduction/substitution failed:
/home/felix/git/libraries/cpp_script/tests/test_list.cpp:96:15: note: couldn’t deduce template parameter ‘T’
96 | List l1(t1);
| ^
我不明白为什么替换失败。 I==Tuple 和 T==Element 应该满足 require 子句就好了。
【问题讨论】:
标签: c++ templates c++20 require sfinae