A compile time ordering on types
<p>I've been looking for a way to get an ordering on types at compile time. This would be useful, for example, for implementing (efficient) compile-time type-sets.</p>

<p>One obvious way to do it would be if there were a way to map every type to a unique integer.  An answer to a <a href="http://stackoverflow.com/a/8111273/626288">previous question</a> on that topic succinctly captures why that's difficult, and it seems like it would apply equally to any other way of trying to get an ordering:</p>

<blockquote>
  <p>the compiler has no way of knowing all compilation units and the linker has no concept of a type</p>
</blockquote>

<p>Indeed, the challenge to the compiler would be considerable: it has to make sure that, in any invocation, for any source file, it returns the same integer for a given type / it returns the same ordering between any two given types, but at the same time, the universe of types is open and it has no knowledge of any types outside of the current file. A hard problem.</p>

<p>The idea I had is that types have names. And by the laws of C++, as far as I know the fully qualified name of a type must be unique across the entire program, otherwise you will get errors or undefined behaviour of some sort or another.</p>

<ul>
<li><p>If two types have the same name, then they are the same type.</p></li>
<li><p>If two types are the same type, then either they have the same name, or they are typedefs for one another. The compiler has full knowledge of typedefs.</p></li>
</ul>

<p>Names are strings, and strings have an ordering. So if I have it right, you could define a globally consistent ordering on types based on their names. More specifically, the ordering between any two types would be the ordering between the names of the types with the typedefs fully resolved. (Having a type behave differently from its typedefs would be problematic.)</p>

<p>Of course, standard C++ doesn't have any facilities for retrieving the names of types.</p>

<p>My questions are:</p>

<ul>
<li><p>Do I have anything wrong? Are there any reasons this wouldn't, in theory, work?</p></li>
<li><p>Are there any compilers which give you access to the names of types (and ideally their typedef-resolved forms) at compile time as a language extension?</p></li>
<li><p>Is there any other way it could be done? Are there any compilers which do?</p></li>
</ul>

<p>(I recognize that it's not polite to ask more than one question in the same question, but it seemed strange to post three separate questions with the same basic throat-clearing preceding them.)</p>
