How to order types at compile-time?

前端 未结 3 1201
陌清茗
陌清茗 2020-12-13 20:48

Consider the following program:

#include 
#include 
#include 
#include 

template 

        
3条回答
  •  猫巷女王i
    2020-12-13 21:39

    tl;dr: Get the type name at compile-time, and order by that.

    Previous answers are, in my opinion, a bit idiosyncratic - at least in implementation.

    At this point we have a very nice, multi-compiler-supporting, function for obtaining a type's name as a compile-time string, as a string view. I'll only quote its signature here:

    template 
    constexpr std::string_view type_name();
    

    This constitutes an injective mapping from types to compile-time-comparable values. Given those, you can easily implement a selection-sort-like procedure to get the relative order of each type. Finally, you assemble a new tuple using those orders.

提交回复
热议问题