반응형
using 사용 형태
① Qualfied Name
#include <stdio.h>
#include <algorithm>
int main()
{
int a = std::max(20, 25); // namespace 명시한 경우
printf("%d", a);
}
② using declaration
#include <stdio.h>
#include <algorithm>
using std::max;
int main()
{
int a = max(20, 25);
printf("%d", a);
}
③ using directive
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
int a = max(20, 25);
printf("%d", a);
}
개인적으로 학습할 때는
③ using directive 형태인 using namespace std; 주로 사용하지만
실무에서는 권장하지 않는다.
예제) using 사용 시 주의사항
#include <stdio.h>
#include <algorithm>
using namespace std;
int count = 10;
int main()
{
int a = max(20, 25);
printf("%d", a);
printf("%d", count); // error
}
전역 변수로 count 변수를 만들었지만 Compile Error 발생한다.
std에는 count 함수가 존재하는데
전역 변수 count인지 std::count인지 알 수 없기 때문이다.
std 안에 어떤 함수가 있는지 모두 외울 수 어려울 뿐더러
컴파일러 종류와 버전에 따라 제공 함수도 다르다.
그래서 실제 업무에서는
① "qualified Name" 형태를 사용하는 것이 좋다.
#include <stdio.h>
#include <algorithm>
int count = 10;
int main()
{
int a = std::max(20, 25);
printf("%d", a);
printf("%d", count);
}
typedef vs using
using은 type에 대한 별칭뿐만 아니라
template 별칭도 처리할 수 있다.
// typedef Case
typedef int INT32
typedef void(*PF)(double)
// using Case
using INT32 = int
using PF = void(*)(double)
반응형
댓글