May. 14th, 2009

zwol: stylized sketch of a face in profile (Default)

Is this well-formed (assume nothing else in the translation unit)?

  template <class T> class R {
    int f();
  };
  template class R<int>;

Now, how about this?

  template <class T> class S {
    int f();
  };
  template <> int S<float>::f() { return 10; }
  template class S<int>;

The crux here being that, in both cases, a definition of S is in scope at the point of the explicit instantiation, but a definition of S::f isn’t (only a specialization, in the second case). Section 14.7 of the C++ standard is so confusing that the authors included a joke about it in the text. GCC doesn’t complain at all, but I don't trust it to get templates exactly pedantically correct.

April 2017

S M T W T F S
      1
2345678
9101112131415
16171819 202122
23242526272829
30      

Style Credit

Expand Cut Tags

No cut tags
Page generated Jan. 6th, 2026 09:04 pm
Powered by Dreamwidth Studios