To singleton or not to singleton, that is the question....
-
@jsulm if void baz() is defined in two namespaces, Calling baz() with using "using Foo;" can cause confusing while two namespaces are present at the same place. Therefore, Foo:: prefix is preferred.
namespace Foo1 { void baz() { ... } } namespace Foo2 { void baz() { ... } } In another class: using Foo1; using Foo2; void AnotheClass::testing() { baz(); // not clear } =================== void AnotheClass::testing() { Foo1::baz(); // clear }
-
I agree that in general you should avoid
using
(for namespaces; now also allowed as a replacement fortypedef
).There are, however, a few select places where I do use it for namespaces: 1) When using
swap
the preferred method is tousing std::swap;
(technically not a namespace) and then callswap
unqualified (without a namespace, class name, etc). 2) For using string""s
and string view""sv
literals. Haven't usedstd::chrono
much, but most likely would import the suffixes as well. -
@jsulm if void baz() is defined in two namespaces, Calling baz() with using "using Foo;" can cause confusing while two namespaces are present at the same place. Therefore, Foo:: prefix is preferred.
namespace Foo1 { void baz() { ... } } namespace Foo2 { void baz() { ... } } In another class: using Foo1; using Foo2; void AnotheClass::testing() { baz(); // not clear } =================== void AnotheClass::testing() { Foo1::baz(); // clear }
@JoeCFD said in To singleton or not to singleton, that is the question....:
Calling baz() with using "using Foo;" can cause confusing while two namespaces are present at the same place.
In the concrete example you have given, it would not compile because the compiler cannot resolve the overload (i.e. Foo1::baz or Foo2::baz). You'd still need to use a fully qualified name. It only gets confusing when you have
Foo1::baz(int)
andFoo2::baz(double)
. You might writebaz(1);
when you actually want to call Foo2::baz. The software might even do the right thing for years. Someone might just introduce Foo1::baz years down the line and suddenly the behavior changes.