Bug 56859 - alignas() fails in template
Summary: alignas() fails in template
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.8.1
: P3 normal
Target Milestone: 4.8.1
Assignee: Jason Merrill
URL:
Keywords:
: 57210 (view as bug list)
Depends on:
Blocks: 58601
  Show dependency treegraph
 
Reported: 2013-04-06 20:38 UTC by Walter Dehnen
Modified: 2016-01-30 22:17 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-04-06 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Walter Dehnen 2013-04-06 20:38:02 UTC
The alignas attribute fails when used with a template-dependent integer constant, as in

template<typename T>
struct alignas(2*sizeof(T)) foo { /* ... */ };

when gcc complains that "error: requested alignment is not an integer constant" (no problem with clang++ 3.2). In a non-templated context, this construct compiles fine.
Comment 1 Paolo Carlini 2013-04-06 22:24:43 UTC
Confirmed. Note the testcase is missing an instantiation, like just "foo<int> f;", required to show the problem.
Comment 2 Jason Merrill 2013-04-09 17:39:22 UTC
Dodji, please take a look at this one too.
Comment 3 Ruben Van Boxem 2013-04-24 19:09:57 UTC
Another simple case that fails (even without instantiation) is:

template<size_type size, size_type alignment>
struct aligned_storage
{
  using type = struct { alignas(alignment) unsigned char data[size]; };
};

from http://en.cppreference.com/w/cpp/types/aligned_storage

The error is:

error: requested alignment is not an integer constant
     using type = struct { alignas(alignment) unsigned char data[size]; };
                                                                     ^
Comment 4 Jason Merrill 2013-04-25 17:50:52 UTC
Fixed.
Comment 5 Paolo Carlini 2013-05-08 13:00:37 UTC
*** Bug 57210 has been marked as a duplicate of this bug. ***
Comment 6 Stephan Tolksdorf 2013-12-11 21:11:30 UTC
The following example still fails with the current trunk GCC. Should this bug be reopened, or should I file a new one?

template <int N>          
void test() {
  constexpr int N2 = N;
  typedef int T alignas(N2);
  // error: requested alignment is not an integer constant
}

int main() {
  test<4>();
  return 0;
}