Nested Namespaces

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

Is this nested namespace syntax standard in C++?

Yes, this is legal C++ 17 syntax. It is, however, not called embedded namespace, but nested namespace.

namespace ns_name::name (8) (since C++17)

[...]

8) nested namespace definition: namespace A::B::C { ... } is equivalent to namespace A { namespace B { namespace C { ... } } }.

Nested NameSpaces in C++

It depends on the namespace you already are:

If you're in no namespace or another, unrelated namespace, then you have to specify to whole path ABC::XYZ::ClassA.

If you're in ABC you can skip the ABC and just write XYZ::ClassA.

Also, worth mentioning that if you want to refer to a function which is not in a namespace (or the "root" namespace), you can prefix it by :::

Example:

int foo() { return 1; }

namespace ABC
{
double foo() { return 2.0; }

void bar()
{
foo(); //calls the double version
::foo(); //calls the int version
}
}

Nested namespaces

Use namespace aliases:

using n2 = n1.n2;

...
n2.foo something;

What is before the class name should be a complete name space (with/or other class name(s) for nested types). A truncated namespace will not work.

How to shorten nested namespaces in a return type which is private to an inner class?

This seems to work:

auto outer::inner::manipulate_foos(footype f1, footype f2) -> footype { ... }

Nested namespaces in i18n

Nested namespaces are not supported.

You can decorate the useTranslation hook to provide this extended functionality for pages in the namespace.

import { useTranslation as useTranslationBase } from "react-i18next";

const useTranslation = (ns, page, props={}) => {
const trans = useTranslationBase(ns, props);

return {
...trans,
t: (keys, options) => {
let _keys = keys;
if (!Array.isArray(keys)) _keys = [String(keys)];
_keys = _keys.map(key =>`${page}.${key}`)
return trans.t(_keys, options)
}
}
}

Usage

export default function () {
const { t } = useTranslation('contract', 'index');
return <div>{t(["pageTitle"])}-{t("pageTitle")}</div>
}

How to forward declare a C++ struct in case nested namespaces are used?

The problem is that you never declared x::y::Container in A.h. You did declare x::y::z::Container, but that doesn't name the same type. Simply move the declaration into the y namespace:

namespace y {
namespace z {

struct Container;

Into ->

namespace y {

struct Container;

namespace z {

Dumb C# Question, How is System and System.Text both namespaces

Your first and second will behave the same. Namespaces are a bit like folders and classes are a bit like files. You could have your namespaces in a hierarchy like folders:

namespace System{
namespace Text{
class StringBuilder{

If you were trying to imagine this as a sequence of DOS commands it might be like every namespace is a mkdir, every { is a cd into it and every } is a cd .. out of it :

mkdir System               (Mkdir doesn't crash if a directory exists)
cd System
mkdir Text
cd Text
edit StringBuilder.cs

You could specify the full path in one hit:

namespace System.Text{
class StringBuilder{

Which could be like

mkdir System\Text         (Yes, mkdir can accept a full path and will create every directory along the way)
cd System\Text
edit StringBuilder.cs

Your third is legal C# but not the same as the other two. Your third declares 3 root level namespaces called System, Text and Linq. A using statement is not like a DOS cd that "leaves you inside namespace X" it's more like "add namespace X to the search path so if you're looking for class Z you have another known path to search"

In DOS terms your third example is like

mkdir System
cd System
cd ..

path add System

mkdir Text
cd Text
cd ..

path add Text

mkdir Linq
cd Linq
cd ..

You don't end up with a hierarchy of folders that way, you end up with 3 folders in C:\

You could write

var sb = new StringBuilder();

But it would be a Text.StringBuilder not a System.Text.StringBuilder because it would be in c:\Text\StringBuilder.cs - it'd still be found when searched for because of the "path add Text" but adding a folder to a search path does not cause folders to be created inside other folders

The file/folder analogy eventually falls down because C# can have any number of classes inside any number of namespaces inside a single file named nothing to do with anything - heck, it could be Cdrive.cs and contain hundreds of classes and namespaces, but for this purpose it'll do..

Accessing a global variables within nested namespaces

1) I though the variables declared in a global scope was... well... global, as in just one instance of it was available to all. So, why/how am I able to declare an instance of a variable with the same name again?

  namespace ns {
int x = 5;

namespace ns_nested {
int z = x; //z is assigned a value of 5??
}
}

here x is not global namespace it is under namespace ns

2)If I were to assign the value of z to the value of x that was declared globally instead of the one in the outer namespace, how would I do it?

see this you may got an idea

#include <iostream>

const int x = 10;

namespace ns
{
int x = 5;

namespace ns_nested
{
int z1 = ::x; //global namespace
int z2 = ns::x; //ns namespace
}
}

int main()
{
std::cout << ns::ns_nested::z1<<std::endl;
std::cout << ns::ns_nested::z2<<std::endl;
}


Related Topics



Leave a reply



Submit