0

I'm trying to do the following:

template <typename type>
struct GetUniqueTypeValue
{
    constexpr static void* get()
    {
        return (void*)&GetUniqueTypeValue::get;
    }

};


constinit static void* record_array[16]{};

constexpr void * sequentialise(void* type_value)
{
    
    
    return nullptr; // I want to return a seuqntialised number here.
}



int main()
{
    constexpr void* type_value = GetUniqueTypeValue<int>::get();
}

I understand the value from typeid() and type_index aren't compile-time values, so that's why I've used the function pointer method, which should be guaranteed to be unique, right? However, I'm stuck at this part. What I want is basically a sequential counter for type IDs, the way that's commonly done at runtime using certain tricks, like just incrementing a counter each time a getID() function is called, or when a static object is initialised at startup. Except I want it to be compile-time.

How can I do this? Edit: What I mean is without feeding each type into a function. I mean like this trick:

struct SequentialiseTypes
{
     static inline int counter = 0;

     template <typename T>
     int getID() { return counter++; }
}

or the other way as:

int counter = 0;
template <typename T>
int ID = counter++;

except at compile time

Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • Do you need the IDs to be unique per: (1) TU, (2) the single executable/shared library, or (3) executable plus all shared libraries? Only (1) is possible at compile-time. – HolyBlackCat Sep 10 '21 at 17:41
  • @HolyBlackCat Per class type, so template struct OuterClass { template int get_sequentialised_unique_id( ) { } }; – Zebrafish Sep 10 '21 at 17:45
  • 1
    That's not what I'm asking. Assume you have two TUs, and each prints a single ID (of different types). Are you ok with them getting the same ID (`0`, that is)? Because there's no way to prevent sequental ID conflicts across TUs. – HolyBlackCat Sep 10 '21 at 17:47
  • @HolyBlackCat If you defined a constexpr function to do this in a header and pasted it in multiple translation units, you're saying there's no way to get the same result, when passing a unique number, or function ptr to the constexpr function, to get the same results for each ptr value or number passed in? – Zebrafish Sep 10 '21 at 17:55
  • 1
    I don't understand the second half of your comment, but yes, of course it's not possible to communicate IDs between TUs, so different TUs will see different IDs. If this is not what you want, you'll have to settle for non-sequental IDs, such as hashes of the type name (which is possible to compute at compile-time). – HolyBlackCat Sep 10 '21 at 18:02
  • @HolyBlackCat type_id or type_index won't give you compile time hashes, will they? Is there a c++ way to do this without using my function pointer method? Or is that OK? Two hashes aren't guaranteed to be different but two function address values, are right? – Zebrafish Sep 10 '21 at 18:08
  • @HolyBlackCat Also if there is a way to do this within one translation unit, would you share it if it's fairly simple to do? – Zebrafish Sep 10 '21 at 18:10
  • See [this answer](https://stackoverflow.com/a/56300605/2752075) for hashing. *"Two hashes aren't guaranteed to be different but two function address values are"* Yes. If this needs to work across DLLs, make sure to check if you get same or different addresses for the same type in different DLLs. *"way to do this within one translation unit ... share it if it's fairly simple"* It would involve stateful metaprogramming, so it's not that simple. See [unconstexpr](https://github.com/DaemonSnake/unconstexpr-cpp20) for inspiration. – HolyBlackCat Sep 10 '21 at 18:21
  • Sequential numbering at module scope (single dll/so/exe itself/etc.) should be doable at link time... may need a linker plugin though. Depends on the linker; I’m only familiar with GNU LD. – numzero Sep 10 '21 at 18:58
  • https://stackoverflow.com/questions/6166337/does-c-support-compile-time-counters ? – KamilCuk Sep 10 '21 at 19:00
  • XY problem? What are you trying to do with these IDs and why do they need to be compile-time and sequential? – n. m. could be an AI Sep 10 '21 at 19:07

1 Answers1

0

No, not without using custom implementation, the language simply does not support that!

If run-time instead of compile-time, and learning a new framework is acceptable, then try Qt (instead custom implementation).

But even in Qt Apps; only types that are registered manually get handled by moc, and can be listed as ID integers (or name strings).

Top-Master
  • 7,611
  • 5
  • 39
  • 71