C++ Academy · Lesson

Transforming Types

remove_const, decay, and more.

Lesson 2 of 413 steps

Transforming Types is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

Type Transformations

Besides querying, <type_traits> can produce new types from old ones. These traits expose a member type ::type, with a _t shortcut in C++14+.

remove_const

std::remove_const_t<const int> yields int. Useful when you need a mutable version of a type.

#include <iostream>
#include <type_traits>

int main() {
    using T = std::remove_const_t<const int>;
    std::cout << std::is_same_v<T, int> << "\n";
    return 0;
}

remove_reference

std::remove_reference_t<int&> gives int. This is central to perfect forwarding and writing generic code.

#include <iostream>
#include <type_traits>

int main() {
    using T = std::remove_reference_t<int&>;
    std::cout << std::is_same_v<T, int> << "\n";
    return 0;
}

add_pointer and add_const

Transformations also add qualifiers.

  • add_const_t<int> = const int
  • add_pointer_t<int> = int*
#include <iostream>
#include <type_traits>

int main() {
    using P = std::add_pointer_t<int>;
    std::cout << std::is_same_v<P, int*> << "\n";
    return 0;
}

std::decay

std::decay_t<T> mimics the conversions that happen when passing by value: it strips references and cv-qualifiers, and converts arrays/functions to pointers.

#include <iostream>
#include <type_traits>

int main() {
    using T = std::decay_t<const int&>;
    std::cout << std::is_same_v<T, int> << "\n";
    return 0;
}

Array Decay

decay turns an array type into a pointer, exactly like passing an array to a function.

#include <iostream>
#include <type_traits>

int main() {
    using T = std::decay_t<int[5]>;
    std::cout << std::is_same_v<T, int*> << "\n";
    return 0;
}

remove_cv

std::remove_cv_t<const volatile int> strips both const and volatile, yielding plain int.

#include <iostream>
#include <type_traits>

int main() {
    using T = std::remove_cv_t<const volatile int>;
    std::cout << std::is_same_v<T, int> << "\n";
    return 0;
}

common_type

std::common_type_t<A,B> gives the type to which both can be converted, like the result of a + b.

#include <iostream>
#include <type_traits>

int main() {
    using T = std::common_type_t<int, double>;
    std::cout << std::is_same_v<T, double> << "\n";
    return 0;
}

underlying_type

For enums, std::underlying_type_t<E> reveals the integer type backing the enum.

#include <iostream>
#include <type_traits>

enum class Color : unsigned char { Red, Green };

int main() {
    using U = std::underlying_type_t<Color>;
    std::cout << std::is_same_v<U, unsigned char> << "\n";
    return 0;
}

Chaining Transforms

Transformations compose. To normalize a forwarded type you often combine them, e.g. remove_cv_t<remove_reference_t<T>>, which is essentially what decay begins with.

Why Transformations Matter

Generic functions frequently receive const T& or T&& but need the bare type to declare locals or return values. Transformation traits give you that clean type.

Quick Check

Recall what decay does.

Recap

You learned type transformations.

  • Traits expose ::type with a _t shortcut
  • remove_const, remove_reference, remove_cv
  • add_pointer, add_const, decay
  • common_type, underlying_type for derived results
Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Transforming Types” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Transforming Types”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

What will I learn in “Transforming Types”?

remove_const, decay, and more. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Transforming Types” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Querying Types
  2. Transforming Types
  3. Conditional Logic
  4. Writing Custom Traits
← Back to C++ Academy