Bug 70124 - alignas error in constexpr function
Summary: alignas error in constexpr function
Status: RESOLVED DUPLICATE of bug 84403
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks: constexpr 58601
  Show dependency treegraph
 
Reported: 2016-03-07 16:27 UTC by Martin Sebor
Modified: 2021-08-28 21:54 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail: 4.9.3, 5.3.0, 6.0
Last reconfirmed: 2016-07-11 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2016-03-07 16:27:39 UTC
Both attribute aligned and the alignas specifier are rejected with an error in a constexpr function complaining that the alignment isn't a constant expression.  Attribute vector_size, on the other hand, is accepted in the same context.

Note also that the diagnostic doesn't mention the value of the (constant) argument or include the call site, making it difficult to determine which invocation of the function caused the error when there is more than one.

$ cat x.c && /build/gcc-trunk-bootstrap/gcc/xg++ -B /build/gcc-trunk-bootstrap/gcc -S -Wall -Wextra -Wpedantic -o/dev/null -xc++ x.c
constexpr int foo (unsigned N)
{
  typedef __attribute__ ((aligned (1 << N))) int I;
  I i = 0;
  return i;
}

int i = foo (__alignof__ (int));

x.c: In function ‘constexpr int foo(unsigned int)’:
x.c:3:50: error: requested alignment is not an integer constant
   typedef __attribute__ ((aligned (1 << N))) int I;
                                                  ^

$ cat x.c && /build/gcc-trunk-bootstrap/gcc/xg++ -B /build/gcc-trunk-bootstrap/gcc -S -Wall -Wextra -Wpedantic -o/dev/null -xc++ x.c
constexpr int foo (unsigned N)
{
  alignas (1 << N) int i = 0;
  return i;
}

int i = foo (__alignof__ (int));

x.c: In function ‘constexpr int foo(unsigned int)’:
x.c:3:17: error: ‘N’ is not a constant expression
   alignas (1 << N) int i = 0;
                 ^
x.c:3:17: error: ‘N’ is not a constant expression
Comment 1 Martin Sebor 2016-03-07 16:38:09 UTC
Bug 70125 tracks the problem with the missing context of the diagnostic.
Comment 2 Andrew Pinski 2016-07-11 04:56:41 UTC
Confirmed.
Comment 3 Marek Polacek 2018-09-06 16:32:08 UTC
Another testcase:

#include <algorithm>

int main() {
        int aa = 0;
        alignas(std::max(alignof(void*), alignof(int))) int bb;
        bb = 1;
}
Comment 4 Andrew Pinski 2021-08-28 21:54:34 UTC
Dup of bug 84403 for the original testcase.


(In reply to Marek Polacek from comment #3)
> Another testcase:

That is valid C++14 and is accepted from GCC 8.5.0, GCC 9.1.0, and GCC 10+.

*** This bug has been marked as a duplicate of bug 84403 ***