Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2004-02-24 04:23:01


"Peter Dimov" <pdimov_at_[hidden]> writes:

>>> Simple: specializations that follow the point of instantiation aren't
>>> considered. This program exits with an error:
>>>
>>> template <class T>
>>> int f(T) { return 1; }
>>>
>>> int main() { return ::f(0); }
>>>
>>> template <> int f(int) { return 0; }
>>
>> It's ill-formed, actually.
>
> On reflection, I think I see what you are trying to illustrate, and I also
> think that you are wrong.

No, my original example illustrated what I was trying to illustrate.
I guess I was just wrong about whether a diagnostic was
required... umm, nope:

  14.7.3 Explicit specialization
  ...
  6 If a template, a member template or the member of a class template
  is explicitly specialized then that specialization shall be declared
  before the first use of that specialization that would cause an
  implicit instantiation to take place, in every translation unit in
  which such a use occurs; no diagnostic is required. If the program
  does not provide a definition for an explicit specialization and
  either the specialization is used in a way that would cause an
  implicit instantiation to take place or the member is a virtual
  member function, the program is ill-formed, no diagnostic
  required. An implicit instantiation is never generated for an
  explicit specialization that is declared but not defined. [Example:

  template<class T> class Array { /* ... */ };
  template<class T> void sort(Array<T>& v) { /* ... */ }
  void f(Array<String>& v)
  {
  sort(v); // use primary template
  // sort(Array<T>&), T is String
  }
  template<> void sort<String>(Array<String>& v); // error: specialization
  // after use of primary template
  template<> void sort<>(Array<char*>& v); // OK: sort<char*> not yet used
  ---end example]

QED

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk