const string in the header file - solutions?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • C. Jayachandran

    const string in the header file - solutions?

    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.

  • Alf P. Steinbach

    #2
    Re: const string in the header file - solutions?

    * C. Jayachandran:[color=blue]
    > 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...[/color]

    The first you should do is _measure_ whether it actually makes a
    difference.

    If it really really matters, then you can use the template constant
    trick (patent pending... ;-) ),

    template< typename T >
    struct Strings_{ static const std::string str; };

    template< typename T >
    std::string const Strings_<T>::st r = "foo";

    typedef Strings_<void> Strings;

    int main()
    {
    std::cout << Strings::str << std::endl;
    }

    Hth.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    Working...