Querying Types
Inspect types at compile time.
Querying Types is a free C++ Academy lesson on CoddyKit — lesson 1 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.
What Are Type Traits?
The <type_traits> header lets you ask questions about types at compile time. Each trait is a template that exposes a constant ::value or a member type.
is_integral
std::is_integral<T>::value is true for integer types like int and char, false otherwise.
#include <iostream>
#include <type_traits>
int main() {
std::cout << std::is_integral<int>::value << "\n";
std::cout << std::is_integral<double>::value << "\n";
return 0;
}The _v Shortcut
C++17 added _v variable templates so you can write std::is_integral_v<T> instead of ::value.
#include <iostream>
#include <type_traits>
int main() {
std::cout << std::is_integral_v<long> << "\n";
std::cout << std::is_floating_point_v<float> << "\n";
return 0;
}Category Traits
Many traits classify types.
is_pointeris_referenceis_arrayis_classis_enum
#include <iostream>
#include <type_traits>
struct S {};
int main() {
std::cout << std::is_pointer_v<int*> << "\n";
std::cout << std::is_class_v<S> << "\n";
std::cout << std::is_reference_v<int&> << "\n";
return 0;
}Relationship Traits
Some traits compare two types.
is_same<A,B>is_base_of<Base,Derived>is_convertible<From,To>
#include <iostream>
#include <type_traits>
struct Base {};
struct Derived : Base {};
int main() {
std::cout << std::is_same_v<int, int> << "\n";
std::cout << std::is_base_of_v<Base, Derived> << "\n";
return 0;
}Property Traits
Property traits report qualifiers and capabilities.
is_constis_signedis_trivially_copyableis_default_constructible
#include <iostream>
#include <type_traits>
int main() {
std::cout << std::is_const_v<const int> << "\n";
std::cout << std::is_signed_v<unsigned> << "\n";
return 0;
}Using Traits in static_assert
Traits combine perfectly with static_assert to enforce requirements at compile time.
#include <type_traits>
#include <iostream>
template <typename T>
T add(T a, T b) {
static_assert(std::is_arithmetic_v<T>, "T must be a number");
return a + b;
}
int main() {
std::cout << add(2, 3) << "\n";
return 0;
}How Traits Work
A trait is just a struct template. The general template inherits false_type; a specialization for the matching type inherits true_type. The compiler picks the best match.
integral_constant
All boolean traits derive from std::integral_constant. true_type and false_type are just aliases for it with value true or false.
#include <iostream>
#include <type_traits>
int main() {
std::cout << std::true_type::value << "\n";
std::cout << std::false_type::value << "\n";
return 0;
}Querying Inside Templates
Traits let templates branch on the kind of type they receive, enabling generic code that adapts to int, float, pointer, or class types differently.
Compile-Time Only
Trait queries cost nothing at runtime; they evaluate during compilation. The result is baked into the generated code.
Quick Check
Recall the C++17 convenience syntax.
Recap
You learned to query types.
<type_traits>answers questions at compile time::valueor the_vshortcut returns a bool- Category, relationship, and property traits
- Pair with
static_assertto enforce constraints
Frequently asked questions
Is the “Querying Types” lesson free?
Yes — the full text of “Querying 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 “Querying Types”?
Inspect types at compile time. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Querying 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
- Querying Types
- Transforming Types
- Conditional Logic
- Writing Custom Traits