N3730
2013-08-28
Mike Spertus
mike_spertus@symantec.com

Specializations and namespaces

Overview

We propose to allow specializing templates from within a different namespace. The motivation is that when we declare a new class, it is natural to want to provide associated template specializations. For example, it is really painful that whenever I declare a class, I need to class all open namespaces and enter namespace std just to specialize std::hash as shown below namespace A { namespace B { /* ... */ class C { /* ... */ }; } } namespace std { template<> struct hash<A::B::C> { size_t operator()(A::B::C const &c) { /* ... */ } }; } namespace A { /* Reenter namespace I am using */ namespace B { /* ... */ } }

Instead, I should be able to specialize std::hash<C> contiguous with the rest of the definition of class C without having to break out of its namespace: namespace A { namespace B { /* ... */ class C { /* ... */ }; template<> struct ::std::hash<C> { std::size_t operator()(C const &c) { /* ... */ } }; /* ... */ } }The technical point is that the primary template identifies the template's namespace, so we don't need to use the namespace enclosing the specialization's definition to identify it's namespace.

We also propose (to be straw-polled separately) the use of friend to put specializations of external templates in a class, just like we allow external friend functions to be defined within a class: namespace A { namespace B { /* ... */ class C { /* ... */ template<> friend struct ::std::hash<C> { std::size_t operator()(C const &c) { /* ... */ } }; }; } }

We also discuss an alternative approach of allowing namespaces to be open in a non-containing namespace.

Motivation

The primary motivation is that the natural place to specialize templates for a class is often alongside the definition of the class.

Specializing in a different namespace

The rules for specializing in a different namespace are fairly straightforward.

The primary template is identified using normal lookup rules

For example, all of the following are OK. For clarity, the varying sections are green. namespace A { namespace B { /* ... */ class C { /* ... */ }; template<> struct ::std::hash<C> { std::size_t operator()(C const &c) { /* ... */ } }; /* ... */ } } namespace A { namespace B { /* ... */ class C { /* ... */ }; template<> struct std::hash<C> { std::size_t operator()(C const &c) { /* ... */ } }; /* ... */ } } using namespace std; namespace A { namespace B { /* ... */ class C { /* ... */ }; template<> struct hash<C> { std::size_t operator()(C const &c) { /* ... */ } }; /* ... */ } }

The same rule applies to declarations using namespace std; namespace A { namespace B { /* ... */ class C { /* ... */ }; template<> struct hash<C>; /* ... */ } }

The current lexical scope is used

I propose using the current lexical scope both because it seems more natural to me, and is more compatible with the rules for defining friend members in §11.3p7. This consistency is particularly nice when we discuss declaring friend specializations below. namespace A { namespace B { /* ... */ class C { /* ... */ }; template<> struct std::hash<C> { // OK std::size_t operator()(C const &c) { /* ... */ } }; struct less<A::B::C> { // Error: Not in namespace std. Also too awkward. bool operator()(C const &c) { /* ... */ } }; /* ... */ } }

All explicit specializations are supported

While the illustrations above have all been fully specializing classes, there is no restriction on the type of specialization. template<class C, int i = 0> struct Foo { std::string foo() { return "foo" }; }; template<class C> struct Bar { std::string bar() { return "bar" }; } namespace A { namespace B { /* ... */ class C { /* ... */ }; template<int i> struct Foo<C, i> { // OK. Partial specialization /* ... */ }; template<> std::string Bar<C>::bar()) { // OK. ordinary method specialization return "Special bar"; }; /* ... */ } }

friend specializations

Just as we allow external functions to be defined as friends (§11.3p6), we allow specializations to be defined as friends. There is one important difference, which is that the specialization is naturally placed in the namespace of the primary template, not in namespace scope (as for function definitions in §11.3p6). namespace A { // Repeated from Overview for easy reference namespace B { /* ... */ class C { /* ... */ template<> friend struct hash<C> { // Specializes std::hash std::size_t operator()(C const &c) { /* ... */ } }; }; } }

Note that this only allows specializations to be defined as friends. Support for declaring classes or primary templates is not required. (However, speaking personally, allowing declarations of friend classes seems worthwhile in conjunction with ADL for template parameters, but this is not required for this proposal...)

Additional functionality: Opening namespaces in non-containing namespaces

This section looks at allowing a namespace to be opened while in a non-containing namespace namespace A { namespace B { /* ... */ class C { /* ... */ }; namespace ::std { template<> struct hash<A::B::C> { size_t operator()(A::B::C const &c) { /* ... */ } }; /* ... */ } } }

This has the benefit of being more general than just allowing specialization in another namespace. For example, if we want to create a header that can be #includeed from within while within a namespace (e.g., to use multiple versions of the same API within a single program), we can write // Foo.h namespace :: { // Make sure we are in global scope #include <string> } /* ... */

While this is nice, I am not recommending adding this functionality for the following reasons: