ヘッダー ファイル内に宣言を入れると問題が発生する可能性があることは理解しているusing
ので、そうしたくありません。代わりに、using
(または a namespace foo =
) をクラス宣言内に配置して、ヘッダー ファイル内での繰り返し入力を削減しようとしました。残念ながら、コンパイル エラーが発生します。便利な機能になりそうです。
#ifndef FOO_H
#define FOO_H
// This include defines types in namespace gee::whiz::abc::def,
// such as the class Hello.
#include "file_from_another_namespace.h"
// using namespace gee::whiz::abc::def; // BAD!
namespace x {
namespace y {
namespace z {
struct Foo {
using namespace gee::whiz::abc::def; // Illegal.
namespace other = gee::whiz::abc::def; // Illegal.
// Foo(gee::whiz::abc::def::Hello &hello); // annoyingly long-winded
Foo(other::Hello &hello); // better
//...
};
} } } // end x::y::z namespace
#endif // FOO_H
実際のコードでは、名前空間名はずっと長くて煩わしく、私が変更できるものではありません。
これが合法ではない理由、または(より良い)回避策があるかどうかを誰かが説明できますか?