I was looking for a way to get type ordering at compile time. This would be useful, for example, for implementing (efficient) sets of compile-time types.
One obvious way to do this would be if there was a way to map each type to a unique integer. The answer to the previous question on this topic briefly reflects why it is difficult, and it seems that it will be applied equally to any other way of receiving an order:
the compiler is not able to know all compilation units, and the linker has no concept of type
In fact, the task for the compiler would be significant: it must make sure that in any call for any source file it returns the same integer for a given type /, it returns the same order between any two given types, but at that while the type universe is open and does not know any types outside the current file. Tough problem.
The idea I had was that types have names. And according to C ++ laws, as far as I know, the full type name must be unique for the entire program, otherwise you will get errors or undefined behavior of one type or another.
If two types have the same name, then they are of the same type.
If two types are of the same type, then they either have the same name, or they are typedefs for each other. The compiler has full knowledge of typedefs.
Names are strings, and strings are in order. Therefore, if everything is correct for me, you can determine a globally consistent order by type based on their names. More specifically, ordering between any two types will be ordering between type names with fully permitted typedefs. (Having a type behaves differently than its typedefs will be problematic.)
Of course, standard C ++ has no means to search for type names.
My questions:
Do I have something wrong? Are there any reasons why this will not work theoretically?
Are there any compilers that give you access to type names (and ideally their forms resolved with typedef) at compile time as an extension of the language?
Is there any other way to do this? Are there compilers that do?
(I admit that he is not polite to ask more than one question in the same question, but it was strange to post three separate questions with the same basic cleanup as before them.)
source share