2

E.g. SFML::Render() vs SFML_Render()

I've noticed in libraries that offer C and C++ bindings, that often the C++ versions make use of namespaces (SFML, libtcod) and the C bindings do the same thing but just prefix the name with what library they belong to.

Both of them require the programmer to prefix the function, both give context as to where they belong, both of them perform the same function. I'm really confused as to what benefits namespaces offer over function prefixing.

Emil Laine
  • 41,598
  • 9
  • 101
  • 157

3 Answers3

4

using-declarations

You can write using SFML::Render after which you can simply call the function with Render(), without needing the SFML:: in the front. The using-declaration can also be scoped to a function or class. This is not possible with prefixed names.

using-directives

You can also bring a whole namespace to the current scope with using namespace. Everyone knows what using namespace std does. These can also be scoped.

Namespace aliases

If you have a symbol with a long qualified name e.g. mylib::sublib::foo::bar::x, you can write namespace baz = mylib::sublib::foo::bar and then refer to x with just baz::x. These are scope-specific as well.

Among the C-style prefixed names there's usually nothing that big that would need an alias, and if there were you could just use a macro.

Adding to & removing from a namespace

If you have a file full of functions that need to be placed under a namespace x you can simply add two lines to make it happen: namespace x { and }. Removing from a namespace is equally simple. With prefixed names you have to manually rename each function.

Argument-dependent lookup

You may be able to omit the namespace qualification on a function call if the function lives in the same namespace as some of its arguments. For example, if namespace baz contains both an enum E and a function F that takes it, you can write F(baz::E) instead of baz::F(baz::E). This can be handy if you follow the style that prefers namespaced free functions over methods.

So in conclusion, namespaces are more flexible and offer more possibilities than the prefixed naming style.

Boojum
  • 6,592
  • 1
  • 30
  • 34
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
  • 1
    Good answer. I'd also add that namespaces can participate in argument-dependent lookup which can be pretty handy. – Boojum Mar 22 '15 at 07:57
  • Awesome, this covers all that I was curious about. Thanks. –  Mar 22 '15 at 08:12
1

SFML_Render is easy to find with fgrep -w SFML_Render, whereas SFML::Render may appear in some source files as just Render if the SFML namespace is implicit.

If you program in C++, there are strong benefits in using the C++ constructs, but you better use a powerful environment such as Eclipse or Visual Studio to help you make sense of all the added complexities.

If you want interoperability with C, you should not use namespaces nor overloading.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
0

they way i see it there are severel uses for namespaces. some of them:

Name isolation: define a package (a set of classes, functions, globals, type definitions, etc.) in a namespace to ensure that when included, these names do not conflict with existing code.

Version control: maintain multiple versions of the code.

and offcourse : A using statement can be used to make names available without the :: scope operator

Ideally, namespaces should

• Express a logical coherent set of features

• Prevent user access to unrelated features.

• Impose minimal notational burden on users.

Matan Perry
  • 67
  • 1
  • 1
  • 6