【问题标题】:Templated requires clause fails模板化 requires 子句失败
【发布时间】: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==TupleT==Element 应该满足 require 子句就好了。

【问题讨论】:

    标签: c++ templates c++20 require sfinae


    【解决方案1】:

    您的示例不起作用,因为无法推断出T 的类型。

    如果你想限制 I 继承基类 Iterable&lt;U&gt; 的某些类型 U,你可能想要这样做

    template <typename U>
    class Iterable { };
    
    template<typename T>
    concept derived_from_iterable = requires (T& x) {
      []<typename U>(const Iterable<U>&){}(x);
    };
    
    class List {
     public:
      template<derived_from_iterable I>
      explicit List(const I& i) {
        // More code
      }
    };
    

    Demo

    【讨论】:

      猜你喜欢
      • 2020-11-29
      • 2020-04-20
      • 2022-01-21
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多