Sort compilation time by type

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.)

+4
source share
1 answer

the full type name must be unique for the entire program

But, of course, this is only true if you consider separate anonymous namespaces in different translation units, in order to have different names in a sense and to have some way of figuring out what they are.

The only meaning in which I know that they really have different names is distorted linker symbols; you can (depending on the compiler) get this from type_info::name() , but this is not guaranteed, limited to types with RTTI and in any case does not appear to be declared as constexpr , so you can't use the value at compile time.

The ordering created by type_info::before() naturally has the same limitations.

Out of interest, what are you trying to achieve with compilation type ordering?

+1
source

Source: https://habr.com/ru/post/1400756/


All Articles