public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "M8R-ug85cr at mailinator dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/55425] New: constexpr does not work in many situations  (both built-in and user supplied)
Date: Wed, 21 Nov 2012 11:31:00 -0000	[thread overview]
Message-ID: <bug-55425-4@http.gcc.gnu.org/bugzilla/> (raw)


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


             reply	other threads:[~2012-11-21 11:31 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-11-21 11:31 M8R-ug85cr at mailinator dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-55425-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).