Constraints and explicit template instantiation

Viewed 104

The following code does not compile in all of gcc 10.1, msvc 19.24, and clang 10.0.0:

template <int N>
struct A {
    void g() requires (N == 3) {}
    void f() requires (N == 3) { g(); }
};

template struct A<2>;

The error is something like

error C7500: 'g': no function satisfied its constraints

I'm somewhat surprised by this. Does this work as intended, is it a defect in a compiler, or is it a defect in the standard?

I was hoping such requires-statement to provide me with member functions which are present conditionally to the values of template arguments. A workaround is the following:

template <int N>
struct A {
    void g() requires (N == 3) {}
    template <bool = true>
    void f() requires (N == 3) { g(); }
};

template struct A<2>;

which compiles as expected, but introduces an extraneous template argument, whose sole purpose is to prevent instantiation.

1 Answers

This seems to be a bug in the compilers; I reported it for all three compilers. Visual Studio has fixed this very recently in 16.7 Preview 1. Gcc developers agreed that it is a bug. No word on Clang yet.

Related