0Pricing
C++ Academy · Lesson

Conditional Logic

std::conditional and if constexpr.

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;
}

All lessons in this course

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