I'm trying to create a map that can work as such:
// I have A, B, and a templated class Processor
struct A;
struct B;
template<class T>
struct Processor {
Processor(T foo);
void SomeFunction();
};
// This is something like how I expect the map to be able to work
int main(){
A a;
B b;
TypeToValMap<KeyValue<A, Processor<A>>,
KeyValue<B, Processor<B>>> myMap{{a}, {b}}; // a and b will be passed to the constructors of the Processors
myMap.get<A>().SomeFunction(); // gets Processor<A> that was constructed with a
myMap.get<B>().SomeFunction(); // gets Processor<B> that was constructed with b
}
I was trying to implement it somewhere along the lines of
template<class Key, class Value>
struct KeyValue{
using key = Key;
using value = Value;
value val;
};
template<typename... Args>
struct TypeToValMap {
TypeToValMap(Args&&... args) {
//???
}
// This function should return the value by using T as the key
template<class T>
constexpr auto get(){ // ???? }
// Needs some container to hold the values?
};
I was trying to use something like
using Procs = std::variant<Processor<A>, Processor<B>>;
std::array<Procs, sizeof...(Args)>
or
std::array<std::any, sizeof...(Args)>
to hold the values, but couldn't get it working. Is it possible to implement such a thing?