C ++에서 "네임 스페이스 별명"이란 무엇입니까?
C ++에서 "네임 스페이스 별명"이란 무엇입니까? 어떻게 사용 되나요?
네임 스페이스 별칭은 다른 짧은 이름으로 긴 네임 스페이스 이름을 참조하는 편리한 방법입니다.
예를 들어, using namespace
지시문 없이 Boost의 uBLAS에서 숫자 벡터를 사용하려한다고 가정하십시오 . 매번 전체 네임 스페이스를 지정하는 것은 번거 롭습니다.
boost::numeric::ublas::vector<double> v;
대신, 당신은 별칭을 정의 할 수 있습니다 boost::numeric::ublas
우리가 이것을 생략하고 싶은 말은 - ublas
:
namespace ublas = boost::numeric::ublas;
ublas::vector<double> v;
간단히 말해 #define이 작동하지 않습니다.
namespace Mine { class MyClass { public: int i; }; }
namespace His = Mine;
namespace Yours { class Mine: public His::MyClass { void f() { i = 1; } }; }
잘 컴파일됩니다. 네임 스페이스 / 클래스 이름 충돌을 해결할 수 있습니다.
namespace Nope { class Oops { public: int j; }; }
#define Hmm Nope
namespace Drat { class Nope: public Hmm::Oops { void f () { j = 1; } }; }
마지막 줄에서 "Hmm : Oops"는 컴파일 오류입니다. 전처리 기는이를 Nope :: Oops로 변경하지만 Nope는 이미 클래스 이름입니다.
또한 네임 스페이스 별명 및 지시문 사용은 런타임이 아니라 컴파일 타임에 해결됩니다. (더 구체적으로 말하면, 현재 범위 또는 상위 범위에서 특정 기호를 찾을 수없는 경우 이름을 확인할 때 찾을 위치를 컴파일러에 알리는 데 사용되는 도구입니다.) 예를 들어, 두 가지 모두 엮다:
namespace A {
int foo;
namespace AA {
int bar;
} // namespace AA
namespace AB {
int bar;
} // namespace AB
} // namespace A
namespace B {
int foo;
namespace BA {
int bar;
} // namespace BA
namespace BB {
int bar;
} // namespace BB
} // namespace B
bool nsChooser1, nsChooser2;
// ...
// This doesn't work.
namespace C = (nsChooser1 ? A : B);
C::foo = 3;
// Neither does this.
// (Nor would it be advisable even if it does work, as compound if-else blocks without braces are easy to inadvertently break.)
if (nsChooser1)
if (nsChooser2)
using namespace A::AA;
else
using namespace A::AB;
else
if (nsChooser2)
using namespace B::BA;
else
using namespace B::BB;
이제 궁금한 점 constexpr
은 컴파일 타임에 변수도 사용되며 별칭 또는 지시문과 함께 사용할 수 있는지 궁금합니다. 내가 아는 한, 나는 이것에 대해 틀릴 수 있지만. 서로 다른 네임 스페이스에서 동일한 이름의 변수로 작업하고 동적으로 선택해야하는 경우 참조 또는 포인터를 사용해야합니다.
// Using the above namespaces...
int& foo = (nsChooser1 ? A::foo : B::foo);
int* bar;
if (nsChooser1) {
if (nsChooser2) {
bar = &A::AA::bar;
} else {
bar = &A::AB::bar;
}
} else {
if (nsChooser2) {
bar = &B::BA::bar;
} else {
bar = &B::BB::bar;
}
}
위의 유용성은 제한 될 수 있지만 목적에 부합해야합니다.
(My apologies for any typoes I may have missed in the above.)
More on this topic http://channel9.msdn.com/Series/C9-Lectures-Stephan-T-Lavavej-Core-C-/Stephan-T-Lavavej-Core-C-1-of-n
It is all about choosing an alias for a looong namespace name, such as:
namespace SHORT = NamespaceFirst::NameSpaceNested::Meow
Then later, you can typedef
typedef SHORT::mytype
instead of
typedef NamespaceFirst::NameSpaceNested::Meow::mytype
This syntax only works for namespaces, cannot include classes, types after the namespace NAME =
Namespace is used to prevent name conflicts.
For example:
namespace foo {
class bar {
//define it
};
}
namespace baz {
class bar {
// define it
};
}
You now have two classes name bar, that are completely different and separate thanks to the namespacing.
The "using namespace" you show is so that you don't have to specify the namespace to use classes within that namespace. ie std::string becomes string.
my resource: https://www.quora.com/What-is-namespace-in-C++-1
참고URL : https://stackoverflow.com/questions/1211399/in-c-what-is-a-namespace-alias
'IT story' 카테고리의 다른 글
Java에서 객체의 크기 계산 (0) | 2020.06.16 |
---|---|
기존 객체에 확장자를 추가하는 Swift 파일의 이름을 지정하는 가장 좋은 방법은 무엇입니까? (0) | 2020.06.16 |
Android 앱에 광고를 삽입 하시겠습니까? (0) | 2020.06.15 |
ng-app와 data-ng-app의 차이점은 무엇입니까? (0) | 2020.06.15 |
파이썬에서 변수가 사전인지 확인하는 방법? (0) | 2020.06.15 |