Providing machinery to allow template class specialisations of classes in other namespaces

50 views
Skip to first unread message

jayru...@gmail.com

unread,
Jul 15, 2017, 10:55:05 AM7/15/17
to ISO C++ Standard - Future Proposals
Hi, first time proposer here, if anyone finds something out of line by all means let me correct myself.

The problem statement is easy: I want to use a library (std or otherwise) and specialise a class within its namespace (a.k.a: std::hash), and I'd like to do it all from my own namespace.

The core issue is that non-trivial amounts of code might be written, and requiring all identifiers to be qualified gets tiresome quickly. It's essentially a pragmatic blocker in the adoption of a more "typeclassy" style of programming. From experience, this affects non-std usage in general (hash is usually pretty terse!).

Specialising a template class inside a different name is currently prohibited. This appears to be an arbitrary restriction, in that the machinery required doesn't exist.

The practical issues are two: providing the compiler the signal that our template specialisation is crossing over to another namespace, and supplying the information required for the name-lookup(s). It is a trivial number of characters to do both (parsing left as an exercise to the reader), but the full ramifications escape me a little. Hence I'm floating it here.

I have written a contrived code sample, and re-used the register keyword because it is terse and it kind of fits (I'm "registering" my class to work with a traits-class/typeclass). Actual syntax is flexible here. Again, throw out suggestions for a different syntax.

namespace mythology
{
struct dragon {};
template <typename T>
struct brilliant_vector;

template <>
register struct std::hash<dragon>
{
// ...
};

// also partial specialization
template <typename T>
register struct ::std::hash<brilliant_vector<T>>
{
// ...
}
}


Let's assume we use the valuable register keyword, simply for brevity and clarity.

  1. The register keyword signifies to the compiler that we are entering a "registering context". This is placed at the front so we can simplify parsing.
  2. Obviously prefixing namespaces in a class definition is illegal in current C++. This would need to be alleviated for these registering contexts.
  3. Let's note that std::hash is fully qualified in the second example. Whilst I can not think of any issues involving allowing unqualified lookup, other than normal ones like ambiguity, this proposal could be restricted to fully-qualified names.
  4. These two specialisations place the specialised definition inside the enclosing namespace of the found identifier. In the first example std::hash is found through usual lookup, and such namespace std is chosen.
  5. Finally, I'll point out there's no reason to forbid partial specialisation.
After all of this, we can write definitions of (e.g.) std::hash whilst inside our own namespace, and use all identifiers with regular name lookup. We're not inside namespace std, we still need to qualify all names inside the std namespace.


This was a bit of a stream of consciousness, so if you're still here, well done you.

Cheers,
 - Jonathan

Michał Dominiak

unread,
Jul 15, 2017, 10:58:08 AM7/15/17
to ISO C++ Standard - Future Proposals
There's currently a paper on this, P0665, which I've presented and that has been favorably received by the EWG, with some further comments on possibly some restrictions as to what can be specialized in what namespaces; there's going to be revisions of that paper, possibly getting looked at by Core in Albuquerque.

--
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposal...@isocpp.org.
To post to this group, send email to std-pr...@isocpp.org.
To view this discussion on the web visit https://groups.google.com/a/isocpp.org/d/msgid/std-proposals/e893cb32-5239-4363-9095-6ce76e310fc9%40isocpp.org.
Reply all
Reply to author
Forward
0 new messages