Context
I'm working on a large project combined from different modules. We've got a exporter with a template function export<T>(const T& obj) which works only on POD types (it does static_assert for is_pod if you're curious). Currently I'm sitting on the part of the system that's responsible for cataloging some entities (their type is irrelevant) that are described by metadata. The metadata itself is returned by some function called metadata describe(const entity& obj), and should be immutable after returning. Of course the function itself sets the metadata members inside its body.
Problem
Due to the facts mentioned above, I need to design a const POD type. Since POD types cannot have user-defined constructors, the member variables themselves cannot be const. Also returning a const variable by value directly from describe is meaningless (or not very helpful to say the least).
Attemted solutions
So basically what I've thought of so far is:
- overload
exporter.export<T>(...)formetadata, but that's not really a solution since it solves only the problem with the current class, while in the final product there will be many types of entities (And I'm not talking about . Overloading the function for all the types seems just wrong. - design an
immutablewrapper and return it fromdescribe. That's what I'm currently doing, since I can't figure out a better way to solve the problem. The wrapper offers an implicit conversion toconst &Tand stores aTinside of itself, thus it can be passed directly to theexportfunction.
Question
Is there a better way to return an immutable POD class from a function? Am I missing something? For simplicity reasons lets assume metadata is defined as follows:
struct metadata{
int parameter1;
time_t parameter2;
};
and describe works as follows (currently, skipping the current solution):
metadata describe(const entity& obj){
metadata m;
m.parameter1 = obj.param1();
m.parameter2 = obj.param2();
return m;
}