Assume I have an enum, and I want each enum value to be associated with a certain type. Say the standard type is double, and if I want it to be something else I need to specify it explicitly.
Q1: Is this the preferred way to implement such a thing?
enum A {
v1,
v2,
v3
};
// for every value of A, the standard type is double
template<A a>
struct A_info {
typedef double type;
};
// other types for certain values can be specified using specialization
template<>
struct A_info<v2> {
typedef size_t type;
};
Then, assume I have some function template, and I want to call the function depending on the type associated with a value of the enum:
template<typename T>
void foo() { /* do something */ }
template<A a>
void bar() {
foo< typename A_info<a>::type >();
}
This works fine. Now assume, I have another function depending on a variadic template list, and I want to do something similar as above ...
template<typename ... T>
void foo_multiple() { /* stuff */ }
template<A ... a>
void bar_multiple() {
foo_multiple< /* ??? */ > ();
}
Q2: How to implement this? Is this possible, anyway?
Just expand the variadic parameter pack with the pattern you want:
template<A ... a>
void bar_multiple() {
foo_multiple<typename A_info<a>::type...> ();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With