if constexpr doesn't play well with Function Concepts

127 views
Skip to first unread message

chedy

unread,
Sep 19, 2016, 8:28:36 PM9/19/16
to SG8 - Concepts
i was playing with if constexpr and Concepts combination, just dummy examples :


#include <iostream>
#include <limits>
using std::numeric_limits;
using std::cout;

template<typename T> concept bool integerType_c =  numeric_limits<T>::is_integer && sizeof(T) == sizeof(int) ;

template<typename T>
concept bool integer_type_c()
{
   
return requires  { sizeof(T) == sizeof(int) && numeric_limits<T>::is_integer;};
}



int main()
{
    int
x = 1;
   
   
if constexpr(integerType_c<decltype(x)>)
        cout
<< "yeah integer";
   
else
        cout
<< "oops pls declare an int";
   
return 0;
}

this works fine : for x being an int it shows me "yeah.." and for x being double it print out "oops..".

but for when using Function Concept it always give me yeah integer no matter the type of x is:

   
    double x = 1;
   
   
if constexpr(integer_type_c<decltype(x)>())
        cout
<< "yeah integer";
   
else
        cout
<< "oops pls declare an int";




is it a bug in GCC? I m using the newest version from Wandbox 7.0.0 Head.

Anton Bikineev

unread,
Sep 19, 2016, 8:35:23 PM9/19/16
to SG8 - Concepts
I believe that your use of requires expression in function concept is not indended.

вторник, 20 сентября 2016 г., 3:28:36 UTC+3 пользователь chedy написал:

chedy najjar

unread,
Sep 19, 2016, 8:38:21 PM9/19/16
to conc...@isocpp.org
what do you mean by indended, can you explain further?

--
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+unsubscribe@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/.

Anton Bikineev

unread,
Sep 19, 2016, 8:56:51 PM9/19/16
to SG8 - Concepts
Sorry, it’s just a typo mixed with my bad English. The expression inside requires expression is a simple requirement. According to TS:
 
 An expression constraint asserts the validity of an expression.

but your expression is perfectly valid, because primary template of numeric_limits resolves everything to false. Try removing requires keyword together with braces to get conjunction of constraint expression.

вторник, 20 сентября 2016 г., 3:38:21 UTC+3 пользователь chedy najjar написал:
To unsubscribe from this group and stop receiving emails from it, send an email to concepts+u...@isocpp.org.

chedy najjar

unread,
Sep 19, 2016, 9:09:22 PM9/19/16
to conc...@isocpp.org
yeah it worked, thx.
so requires is dealing with the syntaxic validity of an expression?

To unsubscribe from this group and stop receiving emails from it, send an email to concepts+unsubscribe@isocpp.org.

Andrew Sutton

unread,
Sep 20, 2016, 11:51:59 AM9/20/16
to conc...@isocpp.org
A requires-expression (the thing requires followed by parens or braces) is concerned with valid syntax. Any other expression is concerned with evaluation. 

And you can also evaluate within a requires-expression:

requires (T x) {
  { f(x) } -> int;  // must be valid, convert to int.
  requires f(x) == 0; // must constexpr evaluate to 0.
}

It's probably more useful with type requirements as in, iterator_t<T> must exist, and oh yeah... it needs to be an Iterator.

Andrew Sutton
Reply all
Reply to author
Forward
0 new messages