I have this class
template<typename T, typename ... Args> class Function
which needs to be declared and initialized this way:
1 2
|
Function<int, int> f1('x', 'y') = {p5, p6};
f1 = {p5, p6};
|
where p5 and p6 are name of functions declared before the main. I am looking for a way to change the method to achieve 2 things:
1) allow the declaration and initialization of the object be integrated into one line, like that:
Function<int, int> f1('x', 'y') = {p5, p6};
2) instead of have to declare some functions and use them to initialize the class, I want use some pre-configured functions (or other type of structure which makes what I want possible), and use them instead the name of functions (like p5, p6 above) this way:
2.1) this pre-configured functions will have names like sin, cos, log, etc, which should be added in the initialization list in two ways: sin('a') or something similar, where 'a' should be only any of the letters used in the declaration(in this example, ´x' or 'y'); or, log(f) or something similar, where 'f' is any of the pre-configured functions mentioned before.
2.2) One of the pre-configured functions I want to have, is one to represent a polynomial term, like C*('x')^E (or something similar), where C is a constant, 'x' is one of the letter used in the declaration or a pre-configured functions, and E is the exponent for the variable 'x'. ^ problably should be overloaded to indicate a exponentiation operation.
The implementation of the class so far is that:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <map>
#include <cstdarg>
#include <vector>
#include <functional>
#include <initializer_list>
template<typename T, typename ... Args>
class Function {
private:
std::map<char,T> va;
std::vector<std::function<T(T, Args...)>> terms;
public:
template<typename... Params>
Function(Params... params) {
for(char c : {params...})
va.insert({c, T()});
}
T operator()(T t, Args ... args) {
T result = 0;
for(long unsigned int i=0; i<terms.size(); i++) result += terms[i](t, args...);
return result;
}
Function& operator=(std::initializer_list<std::function<T(T, Args...)>> array) {
for(long unsigned int i=0; i<array.size(); i++) terms.push_back(std::data(array)[i]);
return *this;
}
};
|
Is it possible to do any of that?