Are there differences between the two practices
Yes. # 1 and # 2 are examples using-directive and namespace definitions respectively. In this case, they are almost identical, but have different consequences. For example, if you enter a new identifier next to MyClass::foo , it will have a different scope:
# one:
using namespace MyNamespace; int x;
# 2:
namespace MyNamespace { int x;
considered better than others?
# 1 Pros: a bit more concise; itβs more difficult to accidentally enter something into MyNamespace involuntarily. Cons: inadvertently pull existing identifiers.
# 2 Pros: It is more clear that the definitions of existing identifiers and the declaration of new identifiers belong to MyNamespace . Cons: It is easier to inadvertently enter identifiers in MyNamespace .
The criticism of both # 1 and # 2 is that they apply to the entire namespace, when you probably only care about defining the members of MyNamespace::MyClass . This is difficult, and he does not communicate intentions well.
A possible alternative # 1 is using-declaration , which includes only the identifier you are interested in:
#include "MyClass.h" using MyNamespace::MyClass; int MyClass::foo() { ... }
John McFarlane Jun 01 '16 at 20:57 2016-06-01 20:57
source share