XMBSMDSJ

2026

< Back to index

C++ Template and Type trait

Two Phase translation

Phase 1

parse the template declaration

Phase 2

Deduce things, instantiate the template. (This happens at each instantiation)

Dependent vs Non-dependent Names

template <typename T>
T::size_type munge(T const &a) {
    T::size_type * i(T::npos);
}

// compiler does not know that size_type is type, npos is constant

// if size_type is type, npos is type
//  T::size_type * i(T::npos) is a function


// if size_type is type, npos is const, object or function
// -> object definition

// if both are not type
// multiplication

How does compiler handle this?

Type trait

Vector pushback

What if T is movable? (Move is faster than copy)

Vector moves items if they are move-constructible