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

            Bug ID: 70667
           Summary: SFINAE error disambiguating using alignas
           Product: gcc
           Version: 6.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: msebor at gcc dot gnu.org
  Target Milestone: ---

The following program tries to use SFINAE to determine whether a non-type
template argument N is a valid alignment.  It depends on overload resolution to
choose between two overloads of function template f, only one of which is valid
for the given value of N.  Since the first overload isn't viable (3 is not a
valid alignment because it isn't a non-negative power of 2), the second
overload is expected to be selected.  But instead, GCC fails with an error.

$ cat u.c && g++ -S -Wall -Wextra -Wpedantic -xc++ u.c
template <int N> struct A { alignas (N) int a; };
template <int N> struct B { char c; };

template <int N> int f (int (*)[sizeof (A<N>)]) { return 0; }
template <int N> int f (int (*)[sizeof (B<N>)]) { return 1; }

int i = f<3>();

u.c: In instantiation of ‘struct A<3>’:
u.c:4:40:   required by substitution of ‘template<int N> int f(int (*)[sizeof
(A<N>)]) [with int N = 3]’
u.c:7:14:   required from here
u.c:1:45: error: requested alignment is not a positive power of 2
 template <int N> struct A { alignas (N) int a; };
                                             ^
u.c:7:14: error: no matching function for call to ‘f()’
 int i = f<3>();
              ^
u.c:4:22: note: candidate: template<int N> int f(int (*)[sizeof (A<N>)])
 template <int N> int f (int (*)[sizeof (A<N>)]) { return 0; }
                      ^
u.c:4:22: note:   substitution of deduced template arguments resulted in errors
seen above
u.c:5:22: note: candidate: template<int N> int f(int (*)[sizeof (B<N>)])
 template <int N> int f (int (*)[sizeof (B<N>)]) { return 1; }
                      ^
u.c:5:22: note:   template argument deduction/substitution failed:
u.c:7:14: note:   candidate expects 1 argument, 0 provided
 int i = f<3>();
              ^

Reply via email to