Querying Types
Inspect types at compile time.
Querying Types is a free C++ Academy lesson on CoddyKit. This is lesson 1 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.
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
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 “Querying Types” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Querying 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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