adding definitions to namespace alias - c++

Is it possible to add something to a namespace alias?
Or how can I achieve the following:
using KdTree = ExternalLibrary::Special::KdTree;
namespace KdTree{
class MySpezial {};
};
That does not work, but the following works:
using KdTree = ExternalLibrary::Special::KdTree;
namespace ExternalLibrary{ namespace Special { namespace KdTree{
class MySpezial {};
}}};
Is the first method just prohibited by the standart?

The first example is currently not allowed and probably won't be in C++1Z either, but note that a recent proposal is allowing
namespace ExternalLibrary::Special::KdTree {
class MySpezial {};
}
This is already implemented in Clang.

Related

conflicting class names in namespace

I have a class which is not part of any namespace
class A(*) .
And I have another class with same name but part of namespace
class A part of namespace B.
In xyz.cpp, I have the below:
#include "..."
using namespace B;
// some code
A::var; // This A should be part of (*) and not namespace B.
// some code
But since I have conflicting class names, I get errors. Is there a way to get around this?
The using namespace keyword imports all of the names from the specified namespace into the global namespace. Since you already declared a class A in the global namespace, this results in a conflict.
Solution: Don't use using namespace B.
This is effectively what you're doing:
namespace GLOBAL {
class A { ... };
};
namespace B {
class A { ... };
};
using namespace B /* export 'B::A' into 'GLOBAL' resulting in a conflict; */ ;
You may not use
using namespace B;
but use like
B::A::var
instead.

How to shorten the namespace indentation in C++ header files without using 'using'?

If you use namespaces for separation of modules / structurization the nesting and indentation within the the header file increases dramatically. Is there a way to write the following code in a shorter way?
namespace A
{
namespace B
{
namespace C
{
namespace D
{
namespace E
{
template <typename T>
public class X
{
public: ...
e.g. like
namespace A::B::C::D::E
{
template<typename T> ...
}
in the header file in c++?
No, that nested namespace syntax has been suggested before at different times and places but isn't valid.
You don't need to indent though
namespace A { namespace B { namespace C {
// ...
} } } // namespace A::B::C
You can use namespace aliasing. This doesn't work for extending existing namespaces, but rather for easier access.
You can use macros to extend existing namespaces, but it you need to do this, you've probably got a deeper namespace hierarchy than you need or want.

Using a namespace only inside one specific class file

This is a rather simple question more or less considering syntax semantics.
I've got a class inside a namespace, which uses a lot of classes out of another namespace:
namespace SomeNamespace
{
class MyClass
{
//...
//These types of namespace uses occur alot around here:
void DoSomething(const anothernamespace::anotherclass &arg);
//...
}
}
This class is of course in its own .hpp file.
I would like to make everything inside the namespace "anothernamespace" available to the MyClass class, however, if I simply put it like this:
namespace SomeNamespace
{
using namespace anothernamespace;
class MyClass
{
//...
//These types of namespace uses occur alot around here:
void DoSomething(const anothernamespace::anotherclass &arg);
//...
}
}
Anyone who does
using namespace SomeNamespace;
Will automatically also use anothernamespace - which is what I want to avoid.
How do I achieve what I want?
The simplest non-perfect-but-helping solution would be to use a namespace alias :
namespace SomeNamespace
{
namespace ans = anothernamespace; // namespace alias
class MyClass
{
//...
//These types of namespace uses occur alot around here:
void DoSomething(const ans::anotherclass &arg);
//...
}
}
Your class users will not "using namespace anothernamespace;", making it more safe, but you still have to use the alias in your class. Not sure it helps, it depends on if you just want to type less or maybe hide a type. Here you put the full namespace in a kind of sub-namespace that don't get into the user's namespaces, but is still available.
Otherwise... there is no way to do exactly what you want. Using namespace don't work inside class declarations.
This does what you want. Both namespaces are accessible to MyClass. using namespace is bad practice in headers though.
namespace SomeNamespace {
namespace other {
using namespace anothernamespace;
class MyClass {
};
}}
namespace SomeNamepace {
typedef other::MyClass MyClass;
}
You really should prefer specifying anothernamespace:: in your class declaration.
You can't. It's just that simple, I'm afraid.

Using a class in a namespace with the same name?

I have to use an API provided by a DLL with a header like this
namespace ALongNameToType {
class ALongNameToType {
static void Foo();
}
}
Is there a way to use ALongNameToType::ALongNameToType::Foo without having to type ALongNameToType::ALongNameToType each time? I tried using using namespace ALongNameToType but got ambiguous symbol errors in Visual Studio. Changing the namespace name or removing it gives me linker errors.
I don't know what's ambiguous, but you can avoid all conflicts with other Foo functions like this:
namespace ALongNameToType {
struct ALongNameToType {
static void Foo();
};
}
typedef ALongNameToType::ALongNameToType Shortname;
int main() {
Shortname::Foo();
}
namespace myns = ALongNameToType;
It seems that you can't alias a class scope like this:
// second ALongNameToType is a class
namespace myns = ALongNameToType::ALongNameToType;
Maybe you could alias the function it self:
namespace foo
{
class foo
{
public:
static void fun()
{
}
};
}
int main()
{
void (*myfunc)() = foo::foo::fun;
myfunc();
}
using ALongNameToType::ALongNameToType::Foo;
if you just want to use it as Foo().
There are three ways to use using. One is for an entire namespace, one is for particular things in a namespace, and one is for a derived class saying it doesn't want to hide something declared/defined in a base class. You can use the second of those:
using ALongNameToType::ALongNameToType
Unfortunately this isn't working for you (due to the ambiguity of the namespace and the class having the same name). Combining this type of using with a previous answer should get rid of the ambiguity:
namespace alntt = ALongNameToType;
using alntt::ALongNameToType;
But once you've renamed the namespace, you really don't need the using statement (as long as you're comfortable writing the (shortened) namespace every time you use the class:
namespace alntt = ALongNameToType;
alntt::ALongNameToType a;
...

Accessing Parent Namespace in C++

I've got a scenario like the following:
class criterion
{
// stuff about criteria...
};
namespace hex {
class criterion : public criterion //does not compile
{ //This should inherit from the
//A hex specific criterion //criterion class in the global namespace
};
};
My question is -- how does one inherit from a class in a namspace which is the parent of another namespace?
Billy3
You need to specify the namespace, in this case the global one:
class criterion : public ::criterion
Note that c++ doesn't specify any means of navigating namespaces as if they were a tree. For example, you can't specify the the "parent" namespace using ".." or any other shorthand - you have to use its name.
Start with "::"
For example
class criterion : public ::criterion {};
Simplified basic C++ namespace rules are:
You can access anything in parent namespace path without specifying namespace.
You can access anything in child namespace path by specifying only relative path.
Everything else requires full namespace specifications.
This compiles for me, basically just explicitly show in what namespace the parent class is:
class A
{};
namespace B {
class A : public ::A
{};
namespace C {
class A : public B::A
{};
}
};