472,372 Members | 1,871 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,372 software developers and data experts.

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.

Feb 6 '06 #1
1 4319
* 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...


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>::str = "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?
Feb 6 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Minti | last post by:
How do we initialize static const data in C++, I tried class Foo { static const std::string name = "MyName"; }; I don't see any reason as to why this must not work.
9
by: Mathieu Malaterre | last post by:
Hello, This thread follow my previous one on the gcc mailing list. Basically I -still- have a problem in my code. I define in a header file: static const std::string foo = "bar"; Which not...
12
by: Riley DeWiley | last post by:
I am looking for a graceful way to declare a string const that is to be visible across many files. If I do this: //----hdr.h const char * sFoo = "foo"; //file.cpp
2
by: srktnc | last post by:
When I run the program, I get a Debug Error saying "This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more...
4
by: C. J. Clegg | last post by:
A month or so ago I read a discussion about putting const ints in header files, and how one shouldn't put things in header files that allocate memory, etc. because they will generate multiple...
4
by: yancheng.cheok | last post by:
Recently, I try to replace #define with const in header file. However, there are concerns on, multiple const object will be created, if the header file is included in multiple cpp files. For...
4
by: Rui.Hu719 | last post by:
Hi, All: I read the following passage from a book: "There are three exceptions to the rule that headers should not contain definitions: classes, const objects whose value is known at compile...
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
3
by: NvrBst | last post by:
I'd like to do something like this but having a problem with the proper syntax in the constructor, maybe someone knows the correct syntax? ---MyClass.h--- #ifndef MYCLASS_H_ #define MYCLASS_H_...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.