0Pricing
C++ Academy · Lesson

Transforming Types

remove_const, decay, and more.

Transforming Types is a free C++ Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Frequently asked questions

Is the “Transforming Types” lesson free?

Yes — the full text of “Transforming Types” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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