I'd like to forgbide the 'using namespace'.
The idea is to force the written of SPACE::class_or_function.
I have problems to find where I have the funtions and classes and, in adittion, I'm going to read my code better.
Is there any way to do this ?
Thanks
Yes there is:
you can write : usingnamespace::class_or_function;
so that when ever you use class_or_function, the compiler will itself attach namespace behind it, but to nothing else.
e.g: using std::cout;
will enable you to use cout, but not cin (which will have to be std::cin).
Nisheeth, I think what he meant was if there was a way to enforce users of a certain library etc to use the namespace::name syntax instead of usingnamespace NAMESPACE;
the answer is - no there isn't. And there shouldn't be. If you don't like usingnamespace - and I'm with you on that - just don't use it. But don't force any particular coding style on your users.
Something that may work: Instead of a namespace use a class.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
class MyNamespace
{
public:
//Add typedefs, static variables and static functions.
typedefunsignedchar MYCHAR;
static MYCHAR myVar;
static MYCHAR MyFunction();
};
//I think the above forces people to use those like this:
MyNamespace::MYCHAR someChar = MyNamespace::MyFunction();
if (someChar == MyNamespace::myVar)
{
//Do stuff.
}