Not sure if this question has been asked before. I searched around and couldn't find an exact answer for my scenario, but I may have missed it.
The issue: I have a namespace in a header file, and some function prototypes inside that namespace in the header:
namespace Foo{
void SomeFunc();
void SomeOtherFunc(int);
}
In the .cpp file, I want to define the functions in the namespace while also providing some protection to an random number generator they all rely on with an anonymous namespace:
namespace Foo{
namespace{
RNG rando = new RNG();
}
void SomeFunc(){
//implementation
}
void SomeOtherFunc(){
//implementation
}
}
The issue I am having is that there is no guarantee that the prototypes in the header file match up to the functions I am defining in the cpp file. Everything in the namespace{} code block can just be something new added to the namespace.
In the above code examples, SomeOtherFunc() is implemented without an argument of type int, but its declaration says it takes some argument. In actuality, it seems I overloaded the SomeOtherFunc() signature.
I could use the following in the cpp file:
namespace Foo{
namespace{
RNG rando = new RNG();
}
}
void Foo::SomeFunc(){
//implementation
}
void Foo::SomeOtherFunc(int){
//implementation
}
The compiler seems to enforce the relationship between the functions' declaration and implementation, which I like. But then the functions seemingly don't have access to the data inside the anonymous namespace.
Am I missing something here? Is there something I could do to resolve the issues I feel are present in this implementation? I am not very well-versed in using (anonymous) namespaces, and so I apologize if this seems basic.