Using contrained-type-specifier as a type of constrained variable

54 views
Skip to first unread message

Anton Bikineev

unread,
Jun 15, 2016, 7:45:52 AM6/15/16
to SG8 - Concepts
Hi SG8,

I haven't found an answer in N4553,but is it possible by design to use constrained-type-specifier to declare constrained variable, like

template <class T>
concept bool callable = ...;

template <class T>
concept bool button = requires (T t, callable c)
{
    {t.on_click(c)};
};

Anyway, it would be of a good use.

Anton Bikineev

unread,
Jun 15, 2016, 7:48:23 AM6/15/16
to SG8 - Concepts
trying to compile this code with gcc-trunk gives the following:

Antons-MacBook-Air:c++ bikineev$ g++ -o 41 41.cc -std=c++1z -fconcepts
41.cc:21:52: internal compiler error: in synthesize_implicit_template_parm, at cp/parser.c:37830
 concept bool button = requires (T t, callable c)
                                    ^

41.cc:21:52: internal compiler error: Abort trap: 6

среда, 15 июня 2016 г., 14:45:52 UTC+3 пользователь Anton Bikineev написал:

Andrew Sutton

unread,
Jun 15, 2016, 8:16:50 AM6/15/16
to conc...@isocpp.org
It isn't specifically allowed and therefore ill-formed (7.1.6.4p8). I think this might be useful, but how would you go determine if a constraint using a placeholder was valid?


--
You received this message because you are subscribed to the Google Groups "SG8 - Concepts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to concepts+u...@isocpp.org.
To post to this group, send email to conc...@isocpp.org.
Visit this group at https://groups.google.com/a/isocpp.org/group/concepts/.
--
Andrew Sutton

Tom Honermann

unread,
Jun 15, 2016, 11:00:03 AM6/15/16
to conc...@isocpp.org
On 6/15/2016 7:48 AM, Anton Bikineev wrote:
trying to compile this code with gcc-trunk gives the following:

Antons-MacBook-Air:c++ bikineev$ g++ -o 41 41.cc -std=c++1z -fconcepts
41.cc:21:52: internal compiler error: in synthesize_implicit_template_parm, at cp/parser.c:37830
 concept bool button = requires (T t, callable c)
                                    ^

41.cc:21:52: internal compiler error: Abort trap: 6

I filed a bug to address the ICE: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71543



среда, 15 июня 2016 г., 14:45:52 UTC+3 пользователь Anton Bikineev написал:
Hi SG8,

I haven't found an answer in N4553,but is it possible by design to use constrained-type-specifier to declare constrained variable, like

template <class T>
concept bool callable = ...;

template <class T>
concept bool button = requires (T t, callable c)
{
    {t.on_click(c)};
};

Anyway, it would be of a good use.

I have wanted to do something like this as well, but I don't have any ideas on how to make it workable.

The typical workaround is to add an additional template parameter for the additional type:

template <class T, class C>
concept bool button =
    callable<C> &&
    requires (T t, C c) {
        {t.on_click(c)};
    };

Tom.

Andrew Sutton

unread,
Jun 15, 2016, 12:03:11 PM6/15/16
to conc...@isocpp.org
I have wanted to do something like this as well, but I don't have any ideas on how to make it workable.

The typical workaround is to add an additional template parameter for the additional type:

template <class T, class C>
concept bool button =
    callable<C> &&
    requires (T t, C c) {
        {t.on_click(c)};
    };

But this isn't the same thing. This requires on_click to be defined for a specific type C. Using a placeholder would require on_click to be defined for *all types* satisfying Callable.


--
Andrew Sutton

Tom Honermann

unread,
Jun 15, 2016, 1:13:12 PM6/15/16
to conc...@isocpp.org
Yes, I know.  This is why I referred to this approach as a workaround and not a solution.  As far as I know, there is no way to accomplish the stated goal in the current design.  The best approximation I've found has been to define the concept as I did above, and then to static assert that the concept is satisfied by a given T with an archetype that models C.  This at least ensures that T isn't restricted to a specific set of types.  But, this also means you have to know what T is, so is only a useful practice for the author of T.

Tom.
Reply all
Reply to author
Forward
0 new messages