Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use a namespace or a struct?

I was just reading a little bit on them from http://www.cplusplus.com/doc/tutorial/namespaces/ and it seems like a struct is capable of the same things? Or even a class for that matter. Maybe someone here can better define what a namespace is, and how it differs from a struct/class?

like image 636
Justen Avatar asked Apr 10 '10 17:04

Justen


People also ask

When should namespaces be used?

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. All identifiers at namespace scope are visible to one another without qualification.

Is a struct a namespace?

A struct (or class) that has only static data and methods is not so different from a namespace. But with a struct that has non-static data, you can create different objects with different data.

When should a struct be used instead of class?

If all the member fields are value types. If instances of the type are small and short-lived or embedded to other instances. If it logically denotes a single value, same as primitive types like int, double, etc. If the size of the instance is below 16 bytes.

Can namespace be declared inside structure?

Namespace definitions cannot appear inside functions; that is, they must appear outside functions at global scope.


2 Answers

Well, seems everyone's going at it, so I'll add my own arguments.

First things first, namespace and struct are completely different beasts: they have different syntax and different semantics.

The obvious:

  • a struct introduces a type, you can use as templates argument
  • a namespace can be spread in several files

Syntactically:

  • both can be "aliased", namespace with namespace ns = mylong::name::space; and struct with typedef mylong::name::Space lilstruct;
  • ADL (or Argument Dependent Lookup) is tailored for namespaces

Semantically:

  • a namespace only defines a scope for the definition of symbols, which allows to group together objects that work together (classes and free-functions) while isolating them for the rest of the world (name clashes). As such it often represents a logical unit of work within the project (for small projects, there is a single namespace).
  • a struct or class defines a logical binding between data and the methods to act upon it, which is the corner stone of encapsulation. It usually has one clear responsability and a number of invariants.

Note that sometimes a struct or class is just used to bind together objects that work together without having any logic, for example struct Person { std::string name, firstName; };.

That being said: there is no point in C++ for a struct of static methods. It's just a perversion from Java or C# and their "pure" OO approach. C++ supports free functions, so there is no point not using them, especially since it's better for encapsulation (they don't have access to private/protected parts, so you can't mess up an invariant and they don't depend on the representation of the class either).

like image 161
Matthieu M. Avatar answered Oct 04 '22 06:10

Matthieu M.


Namespaces and class-types are not capable of the same things. Namespaces are mainly used to group types and functions together to avoid name collisions, while class-types hold data and operations that work on that data.

To just group functions and objects by using a class-types you'd have to make them static:

struct X {
    static void f();
};

Without static you'd have to create instances of the class-types to use them. A namespace is much better suited here:

namespace X {
    void f();
}

Another important thing are using declarations and directives:

namespace X {
    void f();
    void g();
}

void h() {
    using X::f;
    f(); // f() now visible in current scope
    using namespace X;
    f(); g(); // both visible
}

With class-types there simply is no mechanism that allows that.

What class-types give you over namespaces is that you can have multiple instances with differing state - if you need that use a class-type.

like image 34
Georg Fritzsche Avatar answered Oct 04 '22 05:10

Georg Fritzsche