user245019
user245019

Reputation:

Unnamed namespace and Local variables in source files

I always use unnamed namespace inside source files in place of static but I've come across some code that just defines it in the source file.

// Source file foo.cpp

const float someFloat = 3.2f;

Foo::Foo() {
  std::cout << someFloat << std::endl;
}

Is there any disadvantage do doing this over unnamed namespace?

Upvotes: 0

Views: 147

Answers (1)

ComicSansMS
ComicSansMS

Reputation: 54737

In this case some other .cpp file could use extern const float someFloat; to pull that variable in, which is not possible if the variable is static or in an unnamed namespace.

One could argue that the possibility to use extern is a disadvantage as it might break encapsulation in certain situations.

Upvotes: 2

Related Questions