带约束的可变参数模板的“requires”表达式的语法是什么?

问题描述 投票:0回答:1

如果我有一个可变参数模板;

template<typename T>
concept Fooable = requires (T t) { t.bar() -> bool; };

struct Foo {
  int big_foo;

  template<std::Integral T, Fooable ... U>
  explicit Foo(T&& i, U&& ... f) noexcept
    : big_foo {std::forward<T>(i)}
  {
    Something::something(std::forward<U>(f)...);
    ...
  }

};

然后模板的定义及其约束将按预期工作。

但是如果我“要求”对 Foo 有更多约束,因此使用“要求”表达式格式,例如;

  template<typename T, typename ... U>
    requires std::Integral<T>
          && Fooable<U> && BillyBobable<U> // WHAT GOES HERE? ELLIPSIS... BUT WHERE?
  explicit Foo(T&& i, U&& ... f) noexcept
    : big_foo {std::forward<T>(i)}
  {
    SOMETHING::something(std::forward<U>(f)...);
    ...
  }

然后:我应该使用什么作为变量

U
的语法来在表达式中扩展它?

c++ c++20 variadic-templates c++-concepts requires-expression
1个回答
8
投票

您可以在此处使用常用的 C++17 折叠表达式语法:

template<typename T, typename ... U>
  requires std::Integral<T>
        && ((Fooable_concept<U> && BillyBobable_concept<U>) && ...)
explicit Foo(T&& i, U&& ... f) noexcept

或者,您可以通过引入合并两者的新概念来回到之前的语法:

template <typename T>
concept FooAndBillyBob = Fooable_concept<T> && BillyBobable_concept<T>;

template <std::Integral T, FooAndBillyBob ... U>
explicit Foo(T&& i, U&& ... f) noexcept

注意:请不要说出你的概念

*_concept

© www.soinside.com 2019 - 2024. All rights reserved.