Post on 07-Aug-2015
1. C++98 typedef , #define
2. C++11 using (alias declaration)
3. alias template
4. Type Trait Transformation
Effective Modern C++ StudyC++ Korea
#define INT32 inttypedef unsigned char BYTE;typedef unsigned short WORD;
5
#define 은단순문자열치환기능 -> Debugging 힘듬
typedef 는원래타입을숨김 -> 코드이해도하락
Effective Modern C++ StudyC++ Korea
int nVal1 = 2;int nVal2 = 3;
void FUNC_SKY(int args) { std::cout << "Sky : " << args << std::endl; }void FUNC_FLY(int args) { std::cout << "Fly : " << args << std::endl; }
#define MAC_FUNC(F, N) FUNC_##F(nVal##N)
MAC_FUNC(SKY, 2); // Sky : 3
6
## : 앞뒤 문자들을 붙여서 Code에 삽입
Effective Modern C++ StudyC++ Korea
typedef basic_string<char, char_traits<char>, allocator<char>> string;
7
어떤게 더 편할까요 ?std::string strA;std::string strB;
std::basic_string<char, char_traits<char>, allocator<char>> strA;std::basic_string<char, char_traits<char>, allocator<char>> strB;
const std::_Simple_types<std::_Wrap_alloc<std::_Vec_base_types<Widget, std::allocator<Widget> >::_Alloc>::value_type>::value_type *
Effective Modern C++ StudyC++ Korea9
// C++98 typedef
typedef std::unique_ptr<std::unordered_map<std::string, std::string>> UPM_SS;
// C++11 using
using UPM_SS = std::unique_ptr<std::unordered_map<std::string, std::string>>;
에이~ 별 차이 없자나.
Effective Modern C++ StudyC++ Korea10
// C++98 typedef
typedef void(*Func)(int, const std::string&);
// using (alias declaration)
using Func = void(*)(int, const std::string&);
좀 보기 좋긴 한데…
그래도~ 별 차이 없자나.
Effective Modern C++ StudyC++ Korea12
template<typename T> // using (alias declaration)using MyList = std::list<T, MyAlloc<T>>;
template<typename T> // typedefstruct MyList{
typedef std::list<T, MyAlloc<T>> TYPE;};
MyList<Widget> LW;
MyList<Widget>::TYPE LW;
선언이 좀 불편하긴 하네.
근데 사용은 더 불편하다.
Effective Modern C++ StudyC++ Korea14
template 타입의 속성을 변화
std::remove_const<T>::type // T from const Tstd::remove_reference<T>::type // T from T&, T&&std::add_lvalue_reference<T>::type // T& from T
::type ?
누가봐도 template struct typedef 사용한것임을 알수 있다.
Effective Modern C++ StudyC++ Korea15
std::remove_const_t<T> // T from const Tstd::remove_reference_t<T> // T from T&, T&&std::add_lvalue_reference_t<T> // T& from T
이름이 중복 되니깐 _t를 붙여서 사용
Effective Modern C++ StudyC++ Korea16
template<class T>using remove_const_t = typename std::remove_const<T>::type;
template<class T>using remove_refernece_t = typename std::remove_reference_t<T>::type;
template<class T>using add_lvalue_reference_t
= typename std::add_lvalue_reference<T>::type;
Effective Modern C++ StudyC++ Korea17
http://www.cplusplus.com/reference/type_traits
…
Effective Modern C++ StudyC++ Korea19
• using (별칭선언)은 template를 지원합니다.
• using은 ::type 을 뒤에 붙일 필요도 없고,
typename 을 앞에 붙일 필요도 없습니다.
• C++14에서는 C++11의 Type Trait Transformation을
별칭 선언으로 지원합니다.
http://devluna.blogspot.kr/2015/03/item-9-typedef.html
icysword77@gmail.com