Integration between constructor and result of overloaded operator=

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?
Last edited on
What is the purpose of the class template?
In what context do you intend to use it?

You can answer these questions effectively by pretending that you have already written the class template that you're asking for, then providing a complete example of its intended real-world use. This is a useful design exercise for at least two major reasons. First it makes the task concrete -- to define the class template to give the example correct behavior. Second it ensures that a real-world use-case for the intended solution actually exists.

Of course, if it's hard to find a reasonable real-world use case, time is being wasted.

Is it possible to do any of that?

It's possible to do some of it. It would be necessary to change syntax. For example the proposed syntax
Function<int, int> f1('x', 'y') = {p5, p6};
is hopeless because it is forbidden by the language grammar.

There are plenty of alternatives, of which this ugly declaration is just one:
auto f1 = (Function<int, int>('x', 'y') = { p5, p6 });
Last edited on
Topic archived. No new replies allowed.