public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/55425] New: constexpr does not work in many situations  (both built-in and user supplied)
@ 2012-11-21 11:31 M8R-ug85cr at mailinator dot com
  2012-11-21 12:13 ` [Bug c++/55425] constexpr does not work in many situations (both built-in and user supplied literals) redi at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: M8R-ug85cr at mailinator dot com @ 2012-11-21 11:31 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55425

             Bug #: 55425
           Summary: constexpr does not work in many situations  (both
                    built-in and user supplied)
    Classification: Unclassified
           Product: gcc
           Version: 4.7.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: M8R-ug85cr@mailinator.com


constexpr expressions/functions do not work in many cases because of compiler
wrongly interpreting the code. Seen in gcc-MinGW 4.7.0 and 4.7.1.

Situation 1: __func__
--------------------------

A return statement is not a return statement if the returned value is __func__
(also true for non-standard identifiers like __PRETTY_FUNCTION__).

// good
//static const char func[] = "function-name";
//constexpr const char* x() { return func; }

// bad
constexpr const char* x() { return __func__;}

int main() { __builtin_puts(x()); return 0; }

Compiler output:
----------------
error: body of constexpr function 'constexpr const char* x()' not a
return-statement


Cross-reference to corresponding MinGW ticket:
http://sourceforge.net/tracker/index.php?func=detail&aid=3471328&group_id=2435&atid=102435



Situation 2: user literals
--------------------------

The (obviously constant) string that the compiler builds from the literal is
not constant according to the compiler:

#include <stdio.h>

constexpr int valid_bin_number(const char* c) { return *c ? ((*c == '1' || *c
== '0') ? valid_bin_number(c+1) : false ) : true; }

unsigned int operator"" _bin(const char* str)
{
static_assert(valid_bin_number(str), "not a binary number");

unsigned int ret = 0;

for(unsigned int i = 0; str[i] != '\0'; ++i)
{
char digit = str[i];
ret = ret * 2 + (digit - '0');
}
return ret;
}

int main()
{
unsigned int a = 10000_bin;
(void) a;
return 0;
}

Compiler output:
----------------
In function 'unsigned int operator"" _bin(const char*)':
error: non-constant condition for static assertion
error: 'str' is not a constant expression

Cross-reference to corresponding MinGW ticket:
http://sourceforge.net/tracker/?func=detail&atid=102435&aid=3582841&group_id=2435



Situation 3: __m128i type
--------------------------

Assigning a literal value to a constexpr __m128 fails because the literal is
not a literal.

#include <emmintrin.h>

constexpr unsigned int a[] = { 5, 3};      // works (of course)
constexpr float        b[] = { 1.1, 3.7 }; // works, and no warning?!

__m128i                   c[] = { { 0x55633cd9, 0x88ca7a96 }, { 0x0ed8c2a8,
0x7795b179 } };
const __m128i             d[] = { { 0x55633cd9, 0x88ca7a96 }, { 0x0ed8c2a8,
0x7795b179 } };
static const __m128i      e[] = { { 0x55633cd9, 0x88ca7a96 }, { 0x0ed8c2a8,
0x7795b179 } };
namespace { const __m128i f[] = { { 0x55633cd9, 0x88ca7a96 }, { 0x0ed8c2a8,
0x7795b179 } }; }
constexpr __m128i         g[] = { { 0x55633cd9, 0x88ca7a96 }, { 0x0ed8c2a8,
0x7795b179 } };    // <---- fails

int main() { return 0; }


Compiler output:
----------------
error: the type 'const __m128i [] {aka const __vector(2) long long int []}' of
constexpr variable 'g' is not literal


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/55425] constexpr does not work in many situations  (both built-in and user supplied literals)
  2012-11-21 11:31 [Bug c++/55425] New: constexpr does not work in many situations (both built-in and user supplied) M8R-ug85cr at mailinator dot com
@ 2012-11-21 12:13 ` redi at gcc dot gnu.org
  2012-11-21 12:19 ` redi at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2012-11-21 12:13 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55425

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-11-21 12:12:19 UTC ---
(In reply to comment #0)
> 
> A return statement is not a return statement if the returned value is __func__
> (also true for non-standard identifiers like __PRETTY_FUNCTION__).
> 
> // good
> //static const char func[] = "function-name";
> //constexpr const char* x() { return func; }
> 
> // bad
> constexpr const char* x() { return __func__;}
> 
> int main() { __builtin_puts(x()); return 0; }

The standard says __func__ is a function-local variable, defined as if by

constexpr const char* x() {
  static const char __func__[] = "function-name ";
  return __func__;
}

Clearly this is not a valid constexpr function.
Changing this would be an extension.


> Situation 2: user literals
> --------------------------
> 
> The (obviously constant) string that the compiler builds from the literal is
> not constant according to the compiler:
> 
> #include <stdio.h>
> 
> constexpr int valid_bin_number(const char* c) { return *c ? ((*c == '1' || *c
> == '0') ? valid_bin_number(c+1) : false ) : true; }
> 
> unsigned int operator"" _bin(const char* str)
> {
> static_assert(valid_bin_number(str), "not a binary number");

'str' is not a constant expression, so 'valid_bin_number(str)' is not a
constant expression either. This is not a bug.


> Situation 3: __m128i type
> --------------------------
> 
> Assigning a literal value to a constexpr __m128 fails because the literal is
> not a literal.

No, the error says __m128 is not a literal type, which I assume is true.
Changing that would be an enhancement request.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/55425] constexpr does not work in many situations  (both built-in and user supplied literals)
  2012-11-21 11:31 [Bug c++/55425] New: constexpr does not work in many situations (both built-in and user supplied) M8R-ug85cr at mailinator dot com
  2012-11-21 12:13 ` [Bug c++/55425] constexpr does not work in many situations (both built-in and user supplied literals) redi at gcc dot gnu.org
@ 2012-11-21 12:19 ` redi at gcc dot gnu.org
  2012-11-21 12:41 ` jakub at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2012-11-21 12:19 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55425

--- Comment #2 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-11-21 12:19:06 UTC ---
(In reply to comment #1)
> > Situation 3: __m128i type
> > --------------------------
> > 
> > Assigning a literal value to a constexpr __m128 fails because the literal is
> > not a literal.
> 
> No, the error says __m128 is not a literal type, which I assume is true.
> Changing that would be an enhancement request.

With G++ 4.8 __m128i is a literal type, so this example works.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/55425] constexpr does not work in many situations  (both built-in and user supplied literals)
  2012-11-21 11:31 [Bug c++/55425] New: constexpr does not work in many situations (both built-in and user supplied) M8R-ug85cr at mailinator dot com
  2012-11-21 12:13 ` [Bug c++/55425] constexpr does not work in many situations (both built-in and user supplied literals) redi at gcc dot gnu.org
  2012-11-21 12:19 ` redi at gcc dot gnu.org
@ 2012-11-21 12:41 ` jakub at gcc dot gnu.org
  2012-11-21 13:51 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2012-11-21 12:41 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55425

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> 2012-11-21 12:40:29 UTC ---
1) is not valid constexpr IMHO, as the standard says that __func__ works as if
   static const char __func__[] = "function-name";
   has been provided, but when it is provided, it is not valid constexpr.
2) I don't see why you think you could use something like this at all.
   You are defining a non-constexpr operator "", so str there definitely isn't
   pointer to a string literal, it is a standalone function that might be
   inlined if the inliner choses so.  So it is obvious the static_assert
   which must be evaluated before the optimizations, without depending on
   whether the function is inlined or not, must fail.
   You want something like:
constexpr int
cstrlen (const char *c)
{
  return *c ? cstrlen (c + 1) + 1 : 0;
}

constexpr unsigned int
bin_number (const char *c)
{
  return *c ? (*c == '1' ? (1U << cstrlen (c + 1)) : *c == '0' ? 0 : throw 0) +
bin_number (c + 1) : 0;
}

constexpr unsigned int
operator"" _bin (const char *c)
{
  return bin_number (c);
}

int
main ()
{
  constexpr unsigned int a = 10000_bin;
  (void) a;
  return 0;
}

instead, then you can verify it the same at compile time.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/55425] constexpr does not work in many situations  (both built-in and user supplied literals)
  2012-11-21 11:31 [Bug c++/55425] New: constexpr does not work in many situations (both built-in and user supplied) M8R-ug85cr at mailinator dot com
                   ` (2 preceding siblings ...)
  2012-11-21 12:41 ` jakub at gcc dot gnu.org
@ 2012-11-21 13:51 ` jakub at gcc dot gnu.org
  2012-11-21 20:52 ` glisse at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: jakub at gcc dot gnu.org @ 2012-11-21 13:51 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55425

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> 2012-11-21 13:50:43 UTC ---
Or more efficiently
constexpr unsigned int
bin_number (const char *c, unsigned int x)
{
  return *c ? bin_number (c + 1, (x << 1) + (*c == '0' || *c == '1' ? *c - '0'
: throw 0)) : x;
}

constexpr unsigned int
operator"" _bin (const char *c)
{
  return bin_number (c, 0);
}


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/55425] constexpr does not work in many situations  (both built-in and user supplied literals)
  2012-11-21 11:31 [Bug c++/55425] New: constexpr does not work in many situations (both built-in and user supplied) M8R-ug85cr at mailinator dot com
                   ` (3 preceding siblings ...)
  2012-11-21 13:51 ` jakub at gcc dot gnu.org
@ 2012-11-21 20:52 ` glisse at gcc dot gnu.org
  2013-07-01  6:14 ` richard-gccbugzilla at metafoo dot co.uk
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: glisse at gcc dot gnu.org @ 2012-11-21 20:52 UTC (permalink / raw)
  To: gcc-bugs


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55425

Marc Glisse <glisse at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |glisse at gcc dot gnu.org

--- Comment #5 from Marc Glisse <glisse at gcc dot gnu.org> 2012-11-21 20:52:13 UTC ---
(In reply to comment #2)
> (In reply to comment #1)
> > No, the error says __m128 is not a literal type, which I assume is true.
> > Changing that would be an enhancement request.
> 
> With G++ 4.8 __m128i is a literal type, so this example works.

PR 53094 shows that many other vector things don't work with constexpr though.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/55425] constexpr does not work in many situations  (both built-in and user supplied literals)
  2012-11-21 11:31 [Bug c++/55425] New: constexpr does not work in many situations (both built-in and user supplied) M8R-ug85cr at mailinator dot com
                   ` (4 preceding siblings ...)
  2012-11-21 20:52 ` glisse at gcc dot gnu.org
@ 2013-07-01  6:14 ` richard-gccbugzilla at metafoo dot co.uk
  2014-11-19 10:27 ` paolo.carlini at oracle dot com
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: richard-gccbugzilla at metafoo dot co.uk @ 2013-07-01  6:14 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=55425

Richard Smith <richard-gccbugzilla at metafoo dot co.uk> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |richard-gccbugzilla@metafoo
                   |                            |.co.uk

--- Comment #6 from Richard Smith <richard-gccbugzilla at metafoo dot co.uk> ---
(In reply to Jonathan Wakely from comment #1)
> The standard says __func__ is a function-local variable, defined as if by
> 
> constexpr const char* x() {
>   static const char __func__[] = "function-name ";
>   return __func__;
> }
>  
> Clearly this is not a valid constexpr function.
> Changing this would be an extension.

I disagree with this. The standard doesn't say __func__ is only predefined if
it is used, so if this argument held then all constexpr functions would be
invalid.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/55425] constexpr does not work in many situations  (both built-in and user supplied literals)
  2012-11-21 11:31 [Bug c++/55425] New: constexpr does not work in many situations (both built-in and user supplied) M8R-ug85cr at mailinator dot com
                   ` (5 preceding siblings ...)
  2013-07-01  6:14 ` richard-gccbugzilla at metafoo dot co.uk
@ 2014-11-19 10:27 ` paolo.carlini at oracle dot com
  2014-11-19 10:37 ` paolo.carlini at oracle dot com
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: paolo.carlini at oracle dot com @ 2014-11-19 10:27 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55425

--- Comment #7 from Paolo Carlini <paolo.carlini at oracle dot com> ---
I think we can close this. The __func__ snippet now works in mainline with
-std=c++14, I'm adding it to the testsuite.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/55425] constexpr does not work in many situations  (both built-in and user supplied literals)
  2012-11-21 11:31 [Bug c++/55425] New: constexpr does not work in many situations (both built-in and user supplied) M8R-ug85cr at mailinator dot com
                   ` (6 preceding siblings ...)
  2014-11-19 10:27 ` paolo.carlini at oracle dot com
@ 2014-11-19 10:37 ` paolo.carlini at oracle dot com
  2014-11-19 17:41 ` paolo at gcc dot gnu.org
  2014-11-19 17:43 ` paolo.carlini at oracle dot com
  9 siblings, 0 replies; 11+ messages in thread
From: paolo.carlini at oracle dot com @ 2014-11-19 10:37 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55425

--- Comment #8 from Paolo Carlini <paolo.carlini at oracle dot com> ---
Uhm, in fact probably it should be accepted with -std=c++11 too, let's wait a
second.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/55425] constexpr does not work in many situations  (both built-in and user supplied literals)
  2012-11-21 11:31 [Bug c++/55425] New: constexpr does not work in many situations (both built-in and user supplied) M8R-ug85cr at mailinator dot com
                   ` (7 preceding siblings ...)
  2014-11-19 10:37 ` paolo.carlini at oracle dot com
@ 2014-11-19 17:41 ` paolo at gcc dot gnu.org
  2014-11-19 17:43 ` paolo.carlini at oracle dot com
  9 siblings, 0 replies; 11+ messages in thread
From: paolo at gcc dot gnu.org @ 2014-11-19 17:41 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55425

--- Comment #9 from paolo at gcc dot gnu.org <paolo at gcc dot gnu.org> ---
Author: paolo
Date: Wed Nov 19 17:40:42 2014
New Revision: 217788

URL: https://gcc.gnu.org/viewcvs?rev=217788&root=gcc&view=rev
Log:
/cp
2014-11-19  Paolo Carlini  <paolo.carlini@oracle.com>

    PR c++/55425
    * constexpr.c (constexpr_fn_retval): Accept __func__, __FUNCTION__,
    and __PRETTY_FUNCTION__.

/testsuite
2014-11-19  Paolo Carlini  <paolo.carlini@oracle.com>

    PR c++/55425
    * g++.dg/cpp0x/constexpr-__func__.C

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-__func__.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/constexpr.c
    trunk/gcc/testsuite/ChangeLog


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [Bug c++/55425] constexpr does not work in many situations  (both built-in and user supplied literals)
  2012-11-21 11:31 [Bug c++/55425] New: constexpr does not work in many situations (both built-in and user supplied) M8R-ug85cr at mailinator dot com
                   ` (8 preceding siblings ...)
  2014-11-19 17:41 ` paolo at gcc dot gnu.org
@ 2014-11-19 17:43 ` paolo.carlini at oracle dot com
  9 siblings, 0 replies; 11+ messages in thread
From: paolo.carlini at oracle dot com @ 2014-11-19 17:43 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=55425

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |FIXED
   Target Milestone|---                         |5.0

--- Comment #10 from Paolo Carlini <paolo.carlini at oracle dot com> ---
Now we can close this.


^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2014-11-19 17:43 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-11-21 11:31 [Bug c++/55425] New: constexpr does not work in many situations (both built-in and user supplied) M8R-ug85cr at mailinator dot com
2012-11-21 12:13 ` [Bug c++/55425] constexpr does not work in many situations (both built-in and user supplied literals) redi at gcc dot gnu.org
2012-11-21 12:19 ` redi at gcc dot gnu.org
2012-11-21 12:41 ` jakub at gcc dot gnu.org
2012-11-21 13:51 ` jakub at gcc dot gnu.org
2012-11-21 20:52 ` glisse at gcc dot gnu.org
2013-07-01  6:14 ` richard-gccbugzilla at metafoo dot co.uk
2014-11-19 10:27 ` paolo.carlini at oracle dot com
2014-11-19 10:37 ` paolo.carlini at oracle dot com
2014-11-19 17:41 ` paolo at gcc dot gnu.org
2014-11-19 17:43 ` paolo.carlini at oracle dot com

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).