小编Tom*_*ski的帖子

概念可以与模板模板参数一起使用吗?

让我们考虑以下代码:

\n\n
#include <concepts>\n\ntemplate<typename X>\nstruct Concrete_M {\n  X f() const { return X{}; }\n};\n\nstruct Concrete_X {};\n\ntemplate<typename T, typename X>\nconcept M = requires (T t)\n  {\n   { t.f() } -> std::convertible_to<X>;\n  };\n\ntemplate<M<Concrete_X>>\nstruct C {};\n\nconst C<Concrete_M<Concrete_X>> c{};\n
Run Code Online (Sandbox Code Playgroud)\n\n

我可以使用以下模板模板参数吗T

\n\n
template<template<typename> typename T, typename X>\nconcept M = requires (T<X> t)\n  {\n   { t.f() } -> std::convertible_to<X>;\n  };\n
Run Code Online (Sandbox Code Playgroud)\n\n

我该如何改变

\n\n
template<M<Concrete_X>>\nstruct C {};\n\nconst C<Concrete_M<Concrete_X>> c{};\n
Run Code Online (Sandbox Code Playgroud)\n\n

正确使用更新概念M?我正在寻找这样的东西:

\n\n
template<typename X, /* ... */>\nstruct C {};\n\nconst C<Concrete_X, /* ... …
Run Code Online (Sandbox Code Playgroud)

c++ c++-concepts c++20

8
推荐指数
1
解决办法
2175
查看次数

C++20 和(通用)引用中的柯里化

这是我使用 C++20 进行柯里化的实现:

#include <concepts>
#include <functional>

constexpr auto
curry(std::invocable auto f)
{
  return f();
}

constexpr auto
curry(auto f)
{
  return [=](auto x) { return curry(std::bind_front(f, x)); };
}

constexpr int
f(int a, int b, int c)
{
  return a * b * c;
}

constexpr auto
g(auto... args)
{
  return (1 * ... * args);
}

constexpr int
h()
{
  return 42;
}

int
main()
{
  static_assert(curry(f)(2)(3)(7) == 42);
  static_assert(curry(g<int, int, int, int>)(1)(2)(3)(7) == 42);
  static_assert(curry(h) == 42); …
Run Code Online (Sandbox Code Playgroud)

c++ currying forwarding-reference c++20

4
推荐指数
1
解决办法
223
查看次数