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

            Bug ID: 104041
           Summary: static_assert failure triggered by non-selected
                    template specialization
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: bugzilla at cems dot de
  Target Milestone: ---

A static_assert failure can be triggered even by a template specialization that
is not selected. 

The following code selects the specialization C<int> if allowed to compile, but
compilation fails if there is a failing static_assert in the generic template
C<T>, despite it not being selected.


#include <iostream>

template <typename T> struct C
{ C(T) {
#ifdef DEBUG
  static_assert(false, "C<T> selected");
#endif
  std::cout << "C<T> selected"; }
};

template <> struct C<int>
{ C(int) { std::cout << "C<int> selected"; }
};

int main()
{ C c(1);
}

/*
with macro DEBUG defined, compilation error with diagnostic:
error: static assertion failed: C<T> selected

without macro DEBUG defined, compiles and runs with output:
C<int> selected

*/

Reply via email to