Will lite concepts change the need for CRTP to achieve static polymorphism?

As I discovered a CRTPfew years ago, I use it in many places to achieve compile-time polymorphism for very intense computed oriented codes. It's great to "introduce" member functions into classes in a general way when you care about overall and maximum performance at runtime.

I read / looked at a few things on concepts litewhich will (I hope) be part of the following standard C++. It is absolutely wonderful to create functions in a more abstract and general way, avoiding the awfull lines SFINAE/std::enable_ifthat I am currently using.

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

+4
source share
3 answers

. Concepts Lite enable_if, , CRTP. , , .

, CRTP lite. , . , , . :

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

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

Fooable<derived>, . :

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

Fooable<D>() f().

FYI.

+6

, : lite ( CRTP) ? ( ).

- CRTP. lite , :

template<typename T>
concept bool EqualityComparable()
{
    return requires(T a, T b)
    {
        bool = {a == b};
        bool = {a != b};
    };
}

template<InputIterator I, EqualityComparable T>
I find(I first, I last, T x);
// or
template<InputIterator I>
I find(I first, I last, EqualityComparable x);

, lite std::enable_if.

CRTP , , :

template<typename Derived>
void do_something(const CRTP_Base<Derived> &x);

CRTP , : , std::enable_shared_from_this :

class Widget : std::enable_shared_from_this<Widget>
{
    // ...
};

CRTP - , Cloneable:

// Simple example, no covariance, etc
struct Base
{
    typedef unique_ptr<Base> Unique;

    virtual Unique clone() const = 0;
    virtual ~Base() = default;
};

template<typename Derived>
struct ImplementCloneable: protected Base
{
    Unique clone() const override
    {
        return Unique(new Derived(static_cast<const Derived&>(*this)));
    }
protected:
    ~ImplementCloneable() = default;
};

struct Widget: ImplementCloneable<Widget>
{
};
+5

, Concepts Lite " ", , , - . , .

, Concepts Lite. , , , , , (, " " ), .

0

All Articles