This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 114a. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-04-18


2420. Exception specifications in explicit instantiation

Section: 14.5  [except.spec]     Status: dup     Submitter: John Spicer     Date: 2019-06-19

The expected behavior of the following example is not clear:

  template<class T> struct Y {
    typedef typename T::value_type blah;
    void swap(Y<T> &);
  };
  template<class T>
  void swap(Y<T>& Left, Y<T>& Right) noexcept(noexcept(Left.swap(Right))) {
  }

  template <class T> struct Z {
     void swap(Z<T> &);
  };
  template<class T>
  void swap(Z<T>& Left, Z<T>& Right) noexcept(noexcept(Left.swap(Right))) {
  }
  Z<int> x00, y00;
  constexpr bool b00 = noexcept(x00.swap(y00));
  // Instantiates the Z<int> overload:
  template void swap<int>(Z<int>&, Z<int>&) noexcept(b00);

The question is whether the explicit instantiation directive also instantiates the Y<int> overload and thus Y<int> (because of the exception specification), which will fail because of the reference to T::value_type with T=int.

According to 14.5 [except.spec] bullet 13.3, one of the contexts in which an exception specification is needed (thus triggering its instantiation) is when:

the exception specification is compared to that of another declaration (e.g., an explicit specialization or an overriding virtual function);

In this example, the declarations of swap must be compared in order to determine which function template is being instantiated, resulting in the instantiation of Y<int>. There is implementation divergence, however, with some accepting the example and some issuing an error for the instantiation of Y<int>.

Rationale (February, 2022): Duplicate of issue 2417.