I come from the python world where I could define a chain of operations and call them in a for loop:
class AddOne:
def __call__(self, x, **common_kwargs):
return x+1
class Stringify:
def __call__(self, x, **common_kwargs):
return str(x)
class WrapNicely:
def __call__(self, s, **common_kwargs):
return "result="+s
data = 42
for operation in [AddOne(), Stringify(), WrapNicely()]:
data = operation(data)
output = data
(Note: the goal is to have complex operations. Ideally, common kwargs could be given)
What would be the equivalent in C++ if the return type can be different after each call?
I'm not sure I could find anything close but I may have search with wrong keywords…
std::vector
ofstd::function
.std::function
probably won't do the trick here, since different return types are used, that is unless something likestd::variant
is used as parameter/return type...data
cannot change its type, sodata = operation(data)
means the input of the functions is the same as the output (or at least convertable). You can usestd::variant
and similar, but it will not be as dynamic as in python anyway.