Re: A style question on const char* vs. std::string
Zeljko Vrba wrote:
On 2006-12-08, James Kanze <james.kanze@gmail.com> wrote:
Just a question, but would it make sense for the object to be
const. (I typically use something like this for mapping enum
Yes, it would.
Ask them what happens if the object is to be used in the
constructor of a static object:-). (In fact, my normal
Funny you should mention that :) The problem originator (not the person I
was arguing with) attempted to to something like the following:
enum tran_type { ... };
typedef std::map<tran_type, const char*> titles_map;
// he's using class as a namespace for static members
class transaction_ticket {
private:
static titles_map& titles;
static titles_map& get_titles();
...
};
titles_map& transaction_ticket::titles = get_titles(); [*]
titles_map& transaction_ticket::get_titles()
{
static titles_map titles;
static bool titles_initialized = false;
if(!titles_initialized)
{
titles_initialized = true;
// fill the map with code like
titles[..] = "...";
}
return titles;
}
He reported that the program crashed randomly on startup. Sometimes it
worked sometimes it crashed. I have two questions:
1. Why does the assignment marked with [*] compile? I would have expected
that the function call must be transaction_ticket::get_titles().
The initialization expression of a static member variable
definition is evaluated in the context of the class, just as if
it were inside a member function (static, of course, since there
is no this pointer).
2. Crashing is probably due to static initialization order. But where
exactly? [Maybe the fact that transaction_ticket::titles reference and
titles (inside get_titles function) are both static? If the reference
gets initialized before static variable in the get_titles() function
is constructed?]
Who knows where. If any code uses transaction_ticket::titles
before the initialization statement is evaluated, it is
undefined behavior (but in most implementations, a null pointer
will be dereferenced). Presumably, in some other initialization
unit, code in the constructor of a static object is referring to
titles before the initialization in this translation unit has
occurred.
There should be no problem concerning the order of actions in
the definition itself. The local static variable hides the
member reference, so the function cannot refer to the reference
before it is initialized with the return value of the function.
The problem is elsewhere---somebody is refering to the reference
before the function is called.
If all accesses to titles pass through get_titles(), this should
work; it's also a nice trick to have a non-const instance of the
object appear const to all users. But if this were the case, he
wouldn't need the static member titles to begin with.
In a single threaded context, something like:
titles_map const&
transaction_ticket::get_titles()
{
static titles_map theOneAndOnly ;
if ( theOneAndOnly.empty() ) {
titles.insert( titles_map::value_type( ... ) ;
// ...
}
return theOneAndOnly ;
}
is a perfectly valid and usable solution. (One could also argue
that in this case, the map should contain std::string, for
consistency of interface reasons---most maps which don't allow
null pointers will contain std::string, and not char const*.)
In a multi-threaded context, of course, the call to get_titles
is not thread safe, unless you can ensure somehow that the first
call occurs before the threading starts. One way to do this is
to use it in an initialization expression of a static variable
(supposing that threading doesn't start until after main has
started).
--
James Kanze (Gabi Software) email: james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]