In C++, how to use template to accept arbitrary format of aggregate initialization list?

61 Views Asked by At

For instance, consider the function foo, which can be invoced as follows:

foo(
    {{1.0f}, 123},
    {'c', {}}
);

Each argument position within the function can take an aggregate initialization list (Aggregate Initialization), forming a tree-like structure with arbitrary depth.

My realistic goal is to write a template constructor for an arbitrary protobuf class. I believe this can be achieved by combining the template function mentioned previously with dynamic reflection.

Is there a solution available, or is there a reason why this cannot be accomplished?


Thanks for @Eljay, according to the current json libraries(nlomann or taocpp), things can be done by using template class as template arguments of std::initializer_list<T>. I'll give it a try if I could figure out the black magic :'(

0

There are 0 best solutions below