Exception specifications in explicit instantiation
14.5 [except.spec]
John Spicer

Created on 2019-06-19.00:00:00 last changed 19 months ago


Date: 2019-06-19.00:00:00

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>.

Date User Action Args
2019-06-19 00:00:00admincreate