C++ CTAD | Class Template Argument Deduction (C++17)
이 글의 핵심
Learn C++17 CTAD: omit template arguments for pair, vector, and custom types. Deduction guides, explicit constructors, and common pitfalls with initializer_list.
Introduction
CTAD (Class Template Argument Deduction), introduced in C++17, deduces class template arguments from constructor arguments so you can omit explicit template parameters.
1. CTAD basics
C++14 vs C++17
// C++14: explicit types
std::pair<int, double> p(1, 3.14);
std::vector<int> vec = {1, 2, 3};
// C++17: deduced
std::pair p(1, 3.14);
std::vector vec = {1, 2, 3};
Standard library
#include <vector>
#include <map>
#include <tuple>
#include <array>
int main() {
std::pair p(1, "Hello");
std::tuple t(1, 2.0, "Hi");
std::vector vec = {1, 2, 3};
std::map m = {{"a", 1}, {"b", 2}};
}
2. Custom class CTAD
Basic example
template<typename T>
class Container {
T value;
public:
Container(T v) : value(v) {}
T get() const { return value; }
};
int main() {
Container c(42);
Container c2(3.14);
Container c3("Hello");
}
Copy construction
template<typename T>
class Wrapper {
T value;
public:
Wrapper(T v) : value(v) {}
Wrapper(const Wrapper& other) : value(other.value) {}
T get() const { return value; }
};
3. Deduction guides
Simple guide
template<typename T>
class MyClass {
T value;
public:
MyClass(T v) : value(v) {}
};
template<typename T>
MyClass(T) -> MyClass<T>;
Custom conversions
template<typename T>
class Container {
T value;
public:
Container(T v) : value(v) {}
T get() const { return value; }
};
Container(const char*) -> Container<std::string>;
Iterator / nested types
Guides can map (It, It) to vector<value_type_t<It>>—the standard library does this for several constructors.
4. Common pitfalls
Ambiguous deduction
Multiple constructors can compete; guides disambiguate intent.
initializer_list and CTAD
Brace initialization can prefer initializer_list constructors—sometimes surprising; be explicit when needed.
const char* and std::string
Pairs of string literals may deduce to pair<string,string> if you construct that way explicitly; know your overload set.
5. Disabling deduction
explicit constructors do not participate in implicit copy-list-initialization patterns the same way—use explicit types when you must.
6. Examples: smart pointer–style, typed IDs
The article includes SmartPtr(T*) -> SmartPtr<T> and typed identifier wrappers—same code as the Korean version.
Summary
- CTAD deduces class template parameters from constructors (C++17).
- Deduction guides customize that mapping.
- Standard library types commonly use CTAD.
- Cost: compile-time only; no runtime overhead.
Trade-offs
| Pros | Cons |
|---|---|
| Shorter code | Types can be less visible at a glance |
| Still type-safe | Ambiguity if overloads clash |
| Compile-time | Requires C++17+ |
Tips
- Prefer CTAD when types are obvious.
- Spell out template arguments when readability wins.
- Use guides for conversions like
const char*→std::string.
Related posts
Keywords
C++, CTAD, class template argument deduction, deduction guide, C++17.
자주 묻는 질문 (FAQ)
Q. 이 내용을 실무에서 언제 쓰나요?
A. Learn C++17 CTAD: omit template arguments for pair, vector, and custom types. Deduction guides, explicit construct… 실무에서는 위 본문의 예제와 선택 가이드를 참고해 적용하면 됩니다.
Q. 선행으로 읽으면 좋은 글은?
A. 각 글 하단의 이전 글 또는 관련 글 링크를 따라가면 순서대로 배울 수 있습니다. C++ 시리즈 목차에서 전체 흐름을 확인할 수 있습니다.
Q. 더 깊이 공부하려면?
A. cppreference와 해당 라이브러리 공식 문서를 참고하세요. 글 말미의 참고 자료 링크도 활용하면 좋습니다.
같이 보면 좋은 글 (내부 링크)
이 주제와 연결되는 다른 글입니다.
- C++ Deduction Complete Guides | ‘추론 가이드’ 가이드
- C++ if constexpr | ‘컴파일 타임 if’ 가이드
- C++ 초기화 리스트 생성자 | ‘Initializer List’ 가이드
이 글에서 다루는 키워드 (관련 검색어)
C++, CTAD, Templates, C++17, Deduction 등으로 검색하시면 이 글이 도움이 됩니다.