How to constrain on presence of rebind<>

90 views
Skip to first unread message

Andrzej Krzemieński

unread,
Jul 18, 2017, 5:49:47 AM7/18/17
to SG8 - Concepts
Hi Everyone,
I have the following expectation of my types, and I would like to check it with a concept constraint.
Of each type T that pretends to satisfy requirements Rebindable I expect that they have a nested template `rebind` that looks like:

struct R
{
  template <typename T>
    using rebind = /*...*/;
};

That is, `R::rebind<void>` (or `R::template rebind<void>`) or should represent a type (`void` is just an example, but it could be any `T`.

Is there a way to express such expectation in a concept?

Regards,
&rzej;

Vicente BOTET

unread,
Jul 18, 2017, 7:27:43 AM7/18/17
to conc...@isocpp.org

Hi,

 

what you are looking for is universal quantification, and AFAIK concepts don't take in account this case.

 

What you can do is to define a concept RebindableWith<R,T> that is defined if the application of the nested rebind template alias is defined.

 

 

What I have done with TypeConstructible, Functor, Applicative, Monad is to force an explicit trait that states explicitly that the type is a model of the concept. This is cumbersome and doesn't do any syntactical check, but this is what I've do as my best. Of course, if the type is not a model, the compiler messages are as bad asif we had do noting at all.

 

Anyway, if someone knows how to express the Rebindable concept I'm interested also.

 

Vicente

 

> Message du 18/07/17 11:49
> De : "Andrzej Krzemieński" <akrz...@gmail.com>
> A : "SG8 - Concepts" <conc...@isocpp.org>
> Copie à :
> Objet : [concepts] How to constrain on presence of rebind<>
>
>
Hi Everyone,
> I have the following expectation of my types, and I would like to check it with a concept constraint.
> Of each type T that pretends to satisfy requirements Rebindable I expect that they have a nested template `rebind` that looks like:
>
> struct R
> {
>   template
>     using rebind = /*...*/;
> };
>
> That is, `R::rebind` (or `R::template rebind`) or should represent a type (`void` is just an example, but it could be any `T`.

Tom Honermann

unread,
Jul 18, 2017, 11:29:56 AM7/18/17
to conc...@isocpp.org
Does this do what you want?

template<template <typename> class TT>
void is_unary_class_template();

template<typename T>
concept bool Rebindable =
requires {
is_unary_class_template<T::template rebind>();
};

template<typename T>
struct A {
template<typename U>
using rebind = A<U>;
};

static_assert(Rebindable<A<int>>);

Tom.

Andrzej Krzemieński

unread,
Dec 8, 2017, 9:37:18 AM12/8/17
to SG8 - Concepts
Not really. It also accepts invalid types like this one:

template<typename T>
struct B {
   
template<typename U>
   
using rebind = A<U>; // rebind<> should construct a `B`
};

Regards,
&rzej
Reply all
Reply to author
Forward
0 new messages