C++ Academy · Lesson

Conditional Logic

std::conditional and if constexpr.

Lesson 3 of 413 steps

Conditional Logic is a free C++ Academy lesson on CoddyKit. This is lesson 3 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.

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 type
  • if constexpr selects code, discarding the other branch
  • enable_if controls overload resolution via SFINAE
  • Combine traits with logical operators
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 “Conditional Logic” lesson free?

Yes — any 3 lessons of the C++ Academy learning path, including “Conditional Logic”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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

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