Platform Independence

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
User avatar
JamesM
Member
Member
Posts: 2935
Joined: Tue Jul 10, 2007 5:27 am
Location: York, United Kingdom
Contact:

Re: Platform Independence

Post by JamesM »

Solar wrote:
JamesM wrote:

Code: Select all

MyClass::instance().doStuff();
compiles down to the equivalent of:

Code: Select all

MyClass myClass;
myClass.doStuff();
With -O3.
It does? I mean, instance() contains at least a check whether the singleton has been initialized. If you want to be thread-safe, that check would have to be mutexed. I don't say it's impossible to optimize that away, but...

Ah well.
Ah - it doesn't. We don't use a full-on singleton pattern - the singleton pattern is used only so that a variable can be accessed statically from anywhere. The accessor tends to look something like:

Code: Select all

static const MyClass &instance()
{
  return m_Instance;
}
Which of course, can be compiled down into nothingness. I understand your point better now, and yes there would normally be an overhead, but we've done away with this at the expense of no init checks.

James
Post Reply