c++ - return an immutable POD class from a function -


reference

I am working on a large project related to different modules. We have a Exporter A template function Export & lt; T & gt; (Const T & OBG) which works only on POD types (this does static_assert for is_pod if you are keen). Currently I am sitting on the part of the system which is responsible for listing some institutions (their type is irrelevant) which is described by Metadata metadata itself MetaData description (const entity & Obj) is returned by a function called , and should be irreversible after returning. Of course, the function automatically sets metadata members in their body.

Problem

Due to the facts outlined above, because I have the Const POD type since the POD type, the member variable should not be self const Returning const from the convertible value directly to details (or not very useful to say the least).

Solved solutions

So basically what have I thought so far:

  • Overload exporter.export & Lt; T & gt; (...) for metadata , but this is not really a solution because it only resolves the problem with the existing class, while in the final product There will be several types of entities (and I am not talking about it.) The type is just wrong for all .
  • One irreversible code> Cover and embody it with . This is what I am doing, because I'm afraid Can not understand a better way to solve it. In the envelope provides a built-in conversion for const & T and inside a T itself, thus it

question

is a better way to return an unchanging POD class than a function. Is that something I am forgetting? Definition of the metadata from the reasons for simplicity Is done:

  struct metadata {int parameter1; time_t parameter 2; };  

and description works as follows (currently, except for the current solution):

  Metadata description (const entity & Obj) {Metadata i; M.parameter1 = obj.param1 (); M.parameter2 = obj.param2 (); Refund M; }  

You can create member variable const The object needs to start with just an initial list:

  structure metadata {const int1 parameter; ConstTime_T parameter2; };  

and

  Description of metadata (const entity and obj) {return {obj.param1 (), obj.param2 ()}; }  

Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -