模板需要子句失败

发布于 2025-02-09 11:56:29 字数 1547 浏览 1 评论 0原文

我有一组大致定义如下的类:

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 ==元组t ==元素应满足requie条款,就可以了。

I have a set of classes roughly defined as follows:

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
};

Trying to create a List from Tuple as

Tuple t1(1,2,3);
List l1(t1);

gives the following complilation message

/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 don't understand why the substitution fails. I==Tuple and T==Element should satisfy the require clause just fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情徒 2025-02-16 11:56:29

您的示例不起作用,因为没有办法推断出t的类型。

如果要限制i从base类继承iToble&lt; u&gt;对于某些类型u,您可能需要做

template <typename U>
class Iterable { };

template<typename T>
concept derived_from_iterable = requires (T& x) {
  []<typename U>(Iterable<U>&){}(x);
};

class List {
 public:
  template<derived_from_iterable I>
  explicit List(const I& i) {
    // More code
  }
};

demo

Your example won't work because there is no way to deduce the type of T.

If you want to constrain I to inherit from base class Iterable<U> for some type U, you might want to do

template <typename U>
class Iterable { };

template<typename T>
concept derived_from_iterable = requires (T& x) {
  []<typename U>(Iterable<U>&){}(x);
};

class List {
 public:
  template<derived_from_iterable I>
  explicit List(const I& i) {
    // More code
  }
};

Demo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文