반응형
C++ 17부터는 Class Template Argument Deduction이 적용된다. 말 그대로 template argument가 생략될 수 있다는 뜻이다.
적용되는 분야는 다음과 같다.
- template으로 선언 돼 있는 변수를 초기화 할 때 (possibly cv-qualified):
std::pair p{2,2.3}; // pair<int, double>
std::tuple tp{"kor",2,'a'}; // std::make tuple
- new 표현식
template<class T>
struct A
{
A(T, T);
};
auto y = new A{1, 2}; // allocated type is A<int>
- 함수 호출
auto lck = std::lock_guard(mtx); // deduces to std::lock_guard<std::mutex>
std::copy_n(vi1, 3,
std::back_insert_iterator(vi2)); // deduces to std::back_insert_iterator<T>,
// where T is the type of the container vi2
std::for_each(vi.begin(), vi.end(),
Foo([&](int i) {...})); // deduces to Foo<T>,
4. non type template parameter에서도 동작한다. (C++20부터)
template<class T>
struct X
{
constexpr X(T) {}
};
template
struct Y {};
Y<0> y; // OK, Y<X(0)>
TODO:
- [cv-qualified](https://en.cppreference.com/w/cpp/language/cv)
- [on-type template parameter](https://en.cppreference.com/w/cpp/language/template_parameters#Non-type_template_parameter)
Reference
- https://en.cppreference.com/w/cpp/language/class_template_argument_deduction
반응형
'Engineering > C++' 카테고리의 다른 글
Chapter3. Sharing Data Between Threads (1) | 2025.01.20 |
---|---|
Chapter 4. Synchronizing Concurrent Operations (0) | 2025.01.18 |
C++ STL 정리 (2) | 2024.12.19 |
C++에서 auto vs decltype vs typeid (0) | 2024.06.10 |
C++에서 다양한 타입을 벡터 혹은 배열에 저장하는 법 (1) | 2024.06.04 |