I’m trying to implement a generic function that generates a std::string from an id (which is an std::pair<uint32_,uint32_t>).
The function is the following:
typedef uint32_t element_type;
template <element_type type>
std::string to_string (const std::pair<element_type, uint32_t>& id) {
....
const char* name = elemen_type_traits<type>::element_type_name;
...
}
I can invoke the function in the following way:
std::cout << to_string<ELEMENT_TYPE_FOO> (foo_0) << std::endl;
std::cout << to_string<ELEMENT_TYPE_FOO> (foo_1) << std::endl;
The only thing is that I want to make sure that the template parameter matches the first field of the std::pair. Is it possible to deduct the parameter value from std::pair.first?
I don’t know if it’s possible but in the end I would like to have something like this:
std::cout << to_string (foo_0) << std::endl;
std::cout << to_string (foo_1) << std::endl;
Thanks in advance.
If you encode the value inside a type, this is actually achievable:
Live example.
Of course, this only works if the type is always a statically known value, and actually you don’t even need the
id.firstanymore. However, there’s no other way of achieving this check, as far as I know.I personally would probably drop
std::pairand just make a custom struct, together with some other refactoring.Live example.