Conditional Logic
std::conditional and if constexpr.
Conditional Logic is a free C++ Academy lesson on CoddyKit — lesson 3 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.
Choosing Types and Branches
Type-trait logic lets code pick types and code paths at compile time. The key tools are std::conditional and if constexpr.
std::conditional
std::conditional_t<cond, A, B> evaluates to A when cond is true, otherwise B.
#include <iostream>
#include <type_traits>
int main() {
using T = std::conditional_t<true, int, double>;
std::cout << std::is_same_v<T, int> << "\n";
return 0;
}Selecting a Storage Type
A common use: pick a wide type for large values and a narrow one otherwise.
#include <iostream>
#include <type_traits>
template <bool Big>
struct Storage {
using type = std::conditional_t<Big, long long, short>;
};
int main() {
Storage<true>::type a = 10;
std::cout << sizeof(a) << "\n";
return 0;
}if constexpr Basics
if constexpr (C++17) discards the branch not taken at compile time. Only the chosen branch must compile for the given type.
#include <iostream>
#include <type_traits>
template <typename T>
void describe(T v) {
if constexpr (std::is_integral_v<T>)
std::cout << "integer: " << v << "\n";
else
std::cout << "other: " << v << "\n";
}
int main() {
describe(5);
describe(2.5);
return 0;
}Why if constexpr Beats Runtime if
With a regular if, both branches must compile for every T. With if constexpr, the rejected branch is not instantiated, so it may contain type-specific code that would be invalid otherwise.
Type-Specific Operations
This is impossible with a plain if because .length() would not compile for int.
#include <iostream>
#include <string>
#include <type_traits>
template <typename T>
auto size_of(const T& v) {
if constexpr (std::is_same_v<T, std::string>)
return v.length();
else
return sizeof(v);
}
int main() {
std::cout << size_of(std::string("hi")) << "\n";
std::cout << size_of(42) << "\n";
return 0;
}enable_if for Overloads
Before if constexpr, std::enable_if selected overloads via SFINAE. It removes a function from overload resolution when a condition is false.
#include <iostream>
#include <type_traits>
template <typename T, std::enable_if_t<std::is_integral_v<T>, int> = 0>
void only_int(T v) { std::cout << "int " << v << "\n"; }
int main() {
only_int(7);
return 0;
}Combining Conditions
You can combine traits with &&, ||, and ! in compile-time conditions, just like ordinary booleans.
#include <iostream>
#include <type_traits>
template <typename T>
void check() {
if constexpr (std::is_integral_v<T> && !std::is_same_v<T, bool>)
std::cout << "non-bool integer\n";
else
std::cout << "something else\n";
}
int main() {
check<int>();
check<bool>();
return 0;
}Recursive Compile-Time Selection
if constexpr enables compile-time recursion, useful for processing tuples or parameter packs element by element.
conjunction and disjunction
C++17 also provides std::conjunction and std::disjunction for short-circuiting logical AND/OR over a list of traits.
Choosing the Right Tool
Use conditional to select a type, if constexpr to select code, and enable_if/concepts to control overload resolution. Modern code favors if constexpr and concepts for clarity.
Quick Check
Recall the key benefit of if constexpr.
Recap
You learned compile-time conditional logic.
conditional_t<c,A,B>selects a typeif constexprselects code, discarding the other branchenable_ifcontrols overload resolution via SFINAE- Combine traits with logical operators
Frequently asked questions
Is the “Conditional Logic” lesson free?
Yes — the full text of “Conditional Logic” 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 “Conditional Logic”?
std::conditional and if constexpr. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Conditional Logic” 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