I'd like to write a C++11 class that mimics the behavior of a mathematical function. The class takes as an input the sets upon which the function is defined, and it is possible to set and get the value associated with a specific point in the domain.
Since the number of sets that comprise the function domain is not know a priori, I'd like to use C++11 variadic templates to define the class as follows:
template<typename first_set_type, typename... additional_sets_type> class Function;
So that a new function can be created as follows:
Function<int, std::string, double> three_dim_function(S1, S2, S3);
where S1
, S2
and S3
are std::set<int>
, std::set<std::string>
and std::set<double>
, respectively. Setting and getting a value should resemble what happens with std::tuple
:
three_dim_function.set<1, "a", 1.23>(12);
double twelve = three_dim_function.get<1, "a", 1.23>();
Most probably, std::unordered_map
is the ideal data member to store the binding between domain and codomain:
std::unordered_map<std::tuple<first_set_type, additional_set_types...>, double> data_;
I tried to adapt the code from Initialzing and accessing members of a variadic class template, even though the two problems are not identical (in my case I may not need to store each single std::set
).
EDIT #1: I'll try to better stress the issue I'm facing. In the linked question, the class is created by means of recursive calls. However, in my case I'm having troubles in understanding how to implement the constructor, i.e., how to set the domain of the function starting from the input sets. One possible way would be to use the constructor to pre-fill all the keys generated by the Cartesian product of the input sets for the data_
data member. The problem is that I don't know how to iterate over the parameter pack.
Tentative solution #1
Here's a tentative implementation: pastebin.com/FMRzc4DZ based on the contribution of @robert-mason. Unfortunately, it does not compile (clang 4.1, OSX 10.8.4) as soon as is_in_domain()
is called. However, at first sight everything seems fine. What could be wrong?