相关疑难解决方法(0)

使用Concepts Lite为具有成员函数模板的类型指定概念

我正在尝试使用Concepts Lite来指定一个概念来约束具有成员函数模板的更高级的kinded类型.但是,我无法在技术规范教程中找到一个处理概念中模板化语句的子句.

这是怎么做到的?

示例:假设我HKT的成员函数模板具有更高的kinded类型F:

template<class T>
struct HKT {
  template<class U> // this looks like e.g. rebind in std::allocators
  auto F(U) -> HKT<U>;
};
Run Code Online (Sandbox Code Playgroud)

现在我想指定一个约束这些更高级的类型的概念:

template <template <class> class HKT, class T>
concept HKTWithTemplateMemberFunctionF {
  return requires(HKT<T> h) { // HKT<T> is a type, h is an object
    // HKT<T> needs to have a member function template that 
    // returns HTK<U> where the type U is to be deduced and
    // it …
Run Code Online (Sandbox Code Playgroud)

c++ higher-kinded-types c++-concepts

61
推荐指数
2
解决办法
1943
查看次数

如何在 C++20“requires”表达式中使用未指定的类型?

我正在尝试编写一个 C++20 概念来表达类型具有某种方法(该方法接受参数)的要求,但出于此概念的目的,我不关心参数类型是什么。

我试着写一些类似的东西:

template <typename T>
concept HasFooMethod = requires(T t, auto x)
{
    { t.Foo(x) } -> std::same_as<void>;
};
Run Code Online (Sandbox Code Playgroud)

然而,gcc 和 clang 都拒绝这一点,并给出一个错误,即“auto”不能以这种方式在 require 表达式的参数列表中使用。

另一种方法是将“x”的类型作为第二个模板参数:

template <typename T, typename TX>
concept HasFooMethod = requires(T t, TX x)
{
    { t.Foo(x) } -> std::same_as<void>;
};
Run Code Online (Sandbox Code Playgroud)

但这需要在使用该概念时明确指定 TX,因此无法推断:

struct S { void Foo(int); };
static_assert(HasFooMethod<S>);         // doesn't compile
static_assert(HasFooMethod<S, int>);    // the 'int' must be specified
Run Code Online (Sandbox Code Playgroud)

有没有办法编写一个允许 Foo 接受未指定类型的参数的概念?

需要约束模板成员函数的概念定义问题非常相似,但不相同:该问题询问如何要求(模板化)方法可以采用满足给定概念的任何类型,而这个问题是关于要求方法采用某种特定类型,尽管该类型未指定。就量词而言,另一个问题是关于(有界)普遍量化,而这个问题是关于存在量化。另一个问题的答案也不适用于我的情况。

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

3
推荐指数
1
解决办法
1313
查看次数

标签 统计

c++ ×2

c++-concepts ×2

c++20 ×1

higher-kinded-types ×1