Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a namespace private in C++

Consider this case. I am writing a library and want to wrap my data in a namespace. For example:

//header.h
#pragma once

namespace wrapper
{
    // some interface functions here..
}

And I want to make my namespace private. So that no one can write anything in it. For instance, we can always write something like this.

namespace std
{
    // some data here..
}

So I want to prevent the last case. Is there any technique to do that besides using static functions wrapped in a class?

like image 737
Eduard Rostomyan Avatar asked Jul 13 '15 09:07

Eduard Rostomyan


People also ask

Can a namespace be private?

An object namespace protects named objects from unauthorized access. Creating a private namespace enables applications and services to build a more secure environment. A process can create a private namespace using the CreatePrivateNamespace function.

Can we create private class in namespace?

Answer: No. If you try to create a private class in a Namespace, Compiler will throw a compile time error “Namespace elements cannot be explicitly declared as private, protected, or protected internal”.

Can you declare a private class in a namespace C#?

No, Allowing classes to be private to a namespace would achieve no meaningful level of protection. Because private means that the member is only access in the containing class. Since a top-level class has no class containing it; it cannot be private or protected.

What is namespace C?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.


2 Answers

No there isn't. A namespace can always be added to, unless it's an anonymous namespace. But they can only feasibly reside in a single compilation unit.

like image 103
Bathsheba Avatar answered Sep 22 '22 07:09

Bathsheba


This is not possible. If all else fails, I can always edit your header file.

like image 22
Oswald Avatar answered Sep 23 '22 07:09

Oswald