像`declval`这样的概念 [英] Something like `declval` for concepts

查看:65
本文介绍了像`declval`这样的概念的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用模板并使用 decltype 时,即使您没有时间,也经常需要某种类型的实例。在这种情况下, std :: declval< T>()非常有用。这会创建一个 T 类型的假想实例。

When you are working with templates and with decltype you often need an instance of a certain type even though you do not have any the moment. In this case, std::declval<T>() is incredibly useful. This creates an imaginary instance of the type T.

概念是否存在类似的东西?

Is there a something similar for concepts? i.e. a function which would create and imaginary type for a concept.

让我给你举个例子(有点做作,但应该达到目的):

Let me give you an example(a bit contrived but should serve the purpose):

让我们定义一个概念递增

template <typename T>
concept Incrementable = requires(T t){
   { ++t } -> T;                       
};

现在我想有一个概念来测试对象是否具有运算符 operator(),它可以接受递增。用我的虚构语法,我会这样写:

Now I would like to have a concept which tests if an object has the operator operator() which can accept Incrementable. In my imaginary syntax I would write something like this:

template <typename F, typename T = declval<Incrementable>>
concept OperatesOnIncrementable = requires(F f, T t){
   { f(t) } -> T;
} 

declval typename T = declval< Incrementable> 中会创建一个虚构的类型 T ,这实际上不是一个具体类型,但对于所有类型意向和目的的行为就像满足可增量的类型。

There the declval in typename T = declval<Incrementable> would create an imaginary type T which is not really a concrete type but for all intents and purposes behaves like a type which satisfies Incrementable.

即将到来的标准中是否存在一种机制这个?我会发现这非常有用。

Is there a mechanism in the upcoming standard to allow for this? I would find this incredibly useful.

编辑:前段时间,我问了类似的问题,如果可以使用 boost :: hana 完成。

Some time ago I have asked a similar question if this can be done with boost::hana.

编辑:为什么这样做有用?例如,如果您要编写一个包含两个函数的函数

Why is this useful? For example if you want to write a function which composes two functions

template <typename F, typename G>
auto compose(F f, G g) {
  return [f, g](Incrementable auto x) { return f(g(x)); };
}

当我尝试编写两个不能组成。在不限制类型 F G 的情况下,只有在尝试调用组合函数时才会出现错误。

I want to get an error when I try to compose two functions which cannot be composed. Without constraining the types F and G I get error only when I try to call the composed function.

推荐答案


概念是否有相似之处?即将为概念创建虚构类型的函数。

Is there a something similar for concepts? i.e. a function which would create and imaginary type for a concept.

此术语是原型 。提出原型将是非常有价值的功能,并且对于进行定义检查等操作至关重要。从TC的答案来看:

The term for this is an archetype. Coming up with archetypes would be a very valuable feature, and is critical for doing things like definition checking. From T.C.'s answer:


因此,即使您可以神奇地合成某些唯一类型,您也无法保证 F 适用于所有 Incrementable 类型。

做到这一点的方法是合成一个尽可能最少地满足该概念标准的原型。正如他所说,C ++ 20中没有原型生成,鉴于当前的概念化身,这似乎是不可能的。

The way to do that would be to synthesize an archetype that as minimally as possible meets the criteria of the concept. As he says, there is no archetype generation in C++20, and is seems impossible given the current incarnation of concepts.

想出正确的原型非常困难。例如,对于

Coming up with the correct archetype is incredibly difficult. For example, for

template <typename T>
concept Incrementable = requires(T t){
   { ++t } -> T;                       
};

很容易写:

struct Incrementable_archetype {
    Incrementable_archetype operator++();
};

但这并不是尽可能的小-这种类型是默认可构造和可复制的(不是必需的) (code> Incrementable 强加),其 operator ++ 返回正好 T ,这也不是必需的。因此,一个真正的核心原型看起来像:

But that is not "as minimal as possible" - this type is default constructible and copyable (not requirements that Incrementable imposes), and its operator++ returns exactly T, which is also not the requirement. So a really hardcore archetype would look like:

struct X {
    X() = delete;
    X(X const&) = delete;
    X& operator=(X const&) = delete;

    template <typename T> operator,(T&&) = delete;

    struct Y {
        operator X() &&;
    };
    Y operator++();
};

如果您的函数适用于 X ,则它可能适用于所有 Incrementable 类型。如果您的函数不适用于 X ,则可能需要更改实现以使其起作用,或者更改约束以允许更多功能。

If your function works for X, then it probably works for all Incrementable types. If your function doesn't work for X, then you probably need to either change the implementation so it does or change the constraints to to allow more functionality.

有关更多信息,请查看 Boost Concept Check Library 已经很老了,但是至少阅读文档非常有趣。

For more, check out the Boost Concept Check Library which is quite old, but is very interesting to at least read the documentation.

这篇关于像`declval`这样的概念的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆