0

Consider a following piece of code which is essentially an example of Meyer's singleton (hopefully)

static const std::string& foo() // Line 1
{
   static std::string str("foo");
   return str;
}

Is static keyword mentioned in the Line 1 meaningless ? If so why ?

Recker
  • 1,915
  • 25
  • 55

1 Answers1

8

Is static keyword mentioned in the Line 1 meaningless ? If so why ?

It is not meaningless, if you need it or not depend on your situation. static in C++ means different thing in different context, in this case it makes this function only available on current compilation unit. Modern way to do it in C++ - to put the function into anonymous namespace.

Slava
  • 43,454
  • 1
  • 47
  • 90