C
C. Jayachandran
I've inherited some code which has const std::string values defined in
a header file, like
const std::string str = "foo";
This causes a large amount of bloat, as all the compilation units
including this header file will have a copy of the string, as well as
code to construct and destruct the string, even if the string is not
used within the CPP file.
I cannot use the straight-forward solution of converting the 'const' to
'extern const' because this would involve adding a new library, which
is not possible at this point.
The only solution I can think of which will avoid most of the bloat is
to change the definition to:
inline const std::string &get_const_str() { static const std::string s
= "foo"; return s; }
But this would mean turning the const variable to a function. I would
really appreciate any suggestions on how to get a similar effect
without converting the constants to functions, any GCC specific trick
will do too...
Thanks,
JC.
a header file, like
const std::string str = "foo";
This causes a large amount of bloat, as all the compilation units
including this header file will have a copy of the string, as well as
code to construct and destruct the string, even if the string is not
used within the CPP file.
I cannot use the straight-forward solution of converting the 'const' to
'extern const' because this would involve adding a new library, which
is not possible at this point.
The only solution I can think of which will avoid most of the bloat is
to change the definition to:
inline const std::string &get_const_str() { static const std::string s
= "foo"; return s; }
But this would mean turning the const variable to a function. I would
really appreciate any suggestions on how to get a similar effect
without converting the constants to functions, any GCC specific trick
will do too...
Thanks,
JC.