How to save lambda expressions on objects

Hello,

I have an Object, inherited from TObject similar to

class testObject: public TObject {
public:

double A = 5;
std::function<double(double)> fn = [] (double x) -> double {return x;};

ClassDef(testObject, 1);
}

as well as a LinkDef entry. without the lambda expression I can save this to a TFile and open it again, but as soon as I use a lambda expression in the object, I get the error:

Warning in <TStreamerInfo::Build>: GTestObjectLambda: function<double(double)> has no streamer or dictionary, data member "fn" will not be saved
Warning in <TStreamerInfo::Build>: _Maybe_unary_or_binary_function<double,double>: base class unary_function<double,double> has no streamer or dictionary it will not be saved
Warning in <TStreamerInfo::Build>: function<double(double)>: base class _Maybe_unary_or_binary_function<double,double> has no streamer or dictionary it will not be saved
Error in <TStreamerInfo::Build>: _Function_base, unknown type: _Any_data _M_functor

Error in <TStreamerInfo::Build>: _Function_base, unknown type: bool(*)(_Any_data&,const _Any_data&,_Manager_operation) _M_manager

Warning in <TStreamerInfo::Build>: function<double(double)>: base class _Function_base has no streamer or dictionary it will not be saved
Error in <TStreamerInfo::Build>: function<double(double)>, unknown type: double(*)(const _Any_data&,double) _M_invoker

When I use an initializer value for the field where the lambda expression should be everything works fine, but still I want to option to change the expression and save the objects with non default values.

can I some how write an dictionary for that or solve this problem in a different way?

Hi,

the use case why a lambda should be safed isn’t entirely clear to me. In any case, storing functions doesn’t seem foreseen.

What you can do instead is initializing a TFormula from a stored string. With ROOT 6 there is even the complete C++ feature set available inside TFormula.

Cheers,
Benedikt