Import Nested Classes into Namespace - C++

Import nested classes into namespace - C++

You should be able to use namespace aliases for the class:

using B = A::B;

However you can't do that with the member function, not even with static member functions.

Edit: According to this SO answer (What is the difference between 'typedef' and 'using' in C++11) this should be valid, and actually creates a type alias in the same way that typedef does. However, it's C++11 only.


There is a workaround for static member functions in C++11, by declaring a variable pointing to the static function:

struct Foo
{
static void bar()
{ }
};

auto bar = Foo::bar;

Edit: Of course, having a global variable pointing to a static member function is possible in the older C++ standard as well, but it's more messy than using the auto keyword of C++11. In the example above it would be:

void (*bar)() = Foo::bar;

Importing nested namespaces automatically in C#

C# is not Java.

A using directive is used so you don't have to type in the fully qualified name of a type. It also helps with disambiguating type names (using aliases for instance).

In the case of Console, for example, you don't need to type System.Console.

It is important to understand the difference between a namespace and an assembly - a namespace is a logical grouping of types. An assembly is a physical grouping of types. Namespaces can span assemblies.

When you reference an assembly (this is more like importing a package in Java), you gain access to all of the public types in it. In order to use a type you need to uniquely identify it. This is done through the namespace - the using directive simply means you don't have to type the fully qualified name of the type.

Is there a usecase for nested classes?

It allows you to control the access of the nested class- for example, it's often used for implementation detail classes. In C++ it also has advantages in terms of when various things are parsed and what you can access without having to declare first.

Is Reflection a nested namespace inside System namespace in C#?

Yes, System.Reflection is a namespace inside the System namespace. https://msdn.microsoft.com/en-us/library/mt481557(v=vs.110).aspx

Is there a better way to express nested namespaces in C++ within the header

C++17 might simplify nested namespace definition:

namespace A::B::C {
}

is equivalent to

namespace A { namespace B { namespace C {
} } }

See (8) on namespace page on cppreference:

http://en.cppreference.com/w/cpp/language/namespace

Trying to import nested types from class

Since you have no packages, don't use import.

Because JLS §7.5 tells you not to:

A type in an unnamed package (§7.4.2) has no canonical name, so the
requirement for a canonical name in every kind of import declaration
implies that (a) types in an unnamed package cannot be imported, and
(b) static members of types in an unnamed package cannot be imported.
As such, §7.5.1, §7.5.2, §7.5.3, and §7.5.4 all require a compile-time
error on any attempt to import a type (or static member thereof) in an
unnamed package.



Related Topics



Leave a reply



Submit