概念是否会改变CRTP以获得静态多态性的需求? [英] Will concepts lite change the need of CRTP to achieve static polymorphism?

查看:70
本文介绍了概念是否会改变CRTP以获得静态多态性的需求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从几年前我发现CRTP以来,我在很多地方都使用它来实现面向密集计算的代码的编译时多态.当人们在运行时既关心泛型又要获得最大性能时,以一种通用的方式将成员函数注入"到类中是很好的.

Since I have discovered CRTP some years ago, I use it in many places to achieve compile-time polymorphism for very intensive computing oriented codes. It's great to "inject" member functions into classes in a generic way when one cares about both genericity and maximal performances at runtime.

我已经阅读/观看了 concepts lite (希望)成为下一个C++标准的一部分.以一种更加抽象和通用的方式来设计功能,这绝对是一件绝妙的事情,避免了我目前使用的SFINAE/std::enable_if糟糕的说法.

I have read/watch several things on the concepts lite which will be (I hope) a part of the next C++ standard. It will be absolutely wonderfull to design functions in a more abstract and generic manner, avoiding the awfull lines of SFINAE/std::enable_if I currently use.

我还没有测试g++分支,该分支实现了一些概念以与它们一起玩并以一种新的方式研究我喜欢的元编程方法.但是也许你们当中有些人.我的第一个想法是,概念不会解决静态多态性的问题,但是由于这类事情可能严重依赖技巧,因此我可能是错的.所以我的问题是:精简的概念是否能够以更方便的方式实现编译时多态性(就像我们目前可以通过CRTP实现的那样)?(欢迎代码示例)

I have not tested the g++ branches that implements concepts to play with them and to investigate metaprogramming methods I like in a new way. But maybe some of you have. My first thought is that concepts will not solve the problem of static polymorphism, but as these kind of things may heavily relies on tricks, I may be wrong. So my question is the following: will the concepts lite be able to achieve compile-time polymorphism (as we currently can do through CRTP) in a more convenient way ? (examples of code are welcome).

推荐答案

我不应该这样认为. Concepts Lite将取代您对enable_if的使用,但是我不确定它是否允许CRTP的新技术.另一方面,也许可以做一些有趣的事情.

I shouldn't think so. Concepts Lite will replace your use of enable_if, but I'm not sure that it allows for new techniques for CRTP. On the other hand, there may be some interesting things that can be done.

我要说的是,在使用Lite较早实现概念时,我遇到了一些CRTP问题.检查约束要求类型参数必须是完整类型.如果您在派生类上设置了基类的参数,则需要将检查推迟到使用点.例如:

I will say that I ran into some CRTP issues with an earlier implementation of concepts lite. Checking constraints requires the type arguments to be complete types. If you have a base class parameterized over a derived class, you need to defer the check until the point of use. For example:

template<Fooable D>
struct crtp {
  void f() {
    static_cast<D*>(this)->g();
  }
};

struct derived : crtp<derived> { // Error!
};

当您尝试检查Fooable<derived>时,尚未定义派生.最好这样写:

When you try to check Fooable<derived>, derived won't have been defined yet. Better to write it this way:

template<typename D>
struct crtp {
  void f() requires Fooable<D>() {
    static_cast<D*>(this)->g();
  }
};

现在,仅在调用f()时检查Fooable<D>().

Now, Fooable<D>() is only checked when f() is called.

仅供参考.

这篇关于概念是否会改变CRTP以获得静态多态性的需求?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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