Re: [boost] Re: Customization via Overloading in Namespace

Peter Dimov wrote:
David Abrahams wrote:
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. #include <iostream> // f.hpp template<class T> void f(T) { std::cout << "General\n"; } // g.hpp template<class T> void g(T t) { ::f(t); } // X.hpp struct X {}; /*template<>*/ void f(X) { std::cout << "X-specific\n"; } // main.cpp int main() { f( X() ); g( X() ); } Uncomment the /*template<>*/ to see what I mean. No order dependency.

"Peter Dimov" <pdimov@mmltd.net> 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
participants (2)
-
David Abrahams
-
Peter Dimov