Context
I am working on a large project, combined from different modules. We have an exporter with the export<T>(const T& obj) template function export<T>(const T& obj) , which only works for POD types (for is_pod is_pod , if you're interested). Currently, I am sitting on the side of the system, which is responsible for cataloging some objects (their type does not matter), which 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 elements inside its body.
Problem
Due to the facts mentioned above, I need to create a const POD type . Because POD types cannot have user-defined constructors , member variables themselves cannot be const . It is also pointless to return a const variable by value directly from describe (or not very useful to say).
Decisions taken
So basically what I was thinking so far:
- overloading
exporter.export<T>(...) for metadata , but this is not really a solution, since it only solves the problem with the current class, while there will be many types of objects in the final product (And I'm not saying. Function overloading for all types it seems wrong. - create an
immutable wrapper and return it with describe . This is what I am doing now, as I cannot find a better way to solve the problem. The shell offers an implicit conversion to const &T and stores T internally, so it can be passed directly to the export function.
Question
Is there a better way to return an immutable POD class from a function? Am I missing something? For simplicity, suppose 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; }
source share