Question

In my current job, I am seeing variables declared in the unnamed namespace in the cpp file and used only by that class as if they are member variables.

I see it as an interesting way of keeping only interface information in .h and implmentation in .cpp and is less work than the usual pimpl idiom.

I see people using pimpl all the time but never this approach, is there any problem with it?

Was it helpful?

Solution

Variables declared in the unnamed namespace of a .cpp file are file scoped; this means that there is only one instance per execution of the program.

You can see this for yourself by creating two instances of your object and observing that they interfere with each other's variables in the unnamed namespace.

OTHER TIPS

Show some example code please. AFAIK, you cannot declare member variables in the unnamed namespace (unless the class itself is declared in the unnamed namespace).

The unnamed namespace was introduced to replace the common practice of declaring variables as static that are used in just one compilation unit.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top