if
.
, ( C++17), , if . ( C++23).
() if constexpr() ( - () ) -true
|
(1) | ||||||||
() if constexpr() ( - () ) -true else -false
|
(2) | ||||||||
() if !() consteval -
|
(3) | ( C++23) | |||||||
() if !() consteval - else
|
(4) | ( C++23) | |||||||
| ( C++11) | ||||
constexpr
|
( C++17) , constexpr if | |||
| - | ( C++17)
| |||
| - | ( ), , true
| |||
| - | ( ), , false
| |||
| , , if | ||||
| ( , ), , if |
true bool, -.
else if false bool, -.
if (, else), - if, if else ( , if, else if, else).
#include <iostream>
int main()
{
// if else
int i = 2;
if (i > 2)
std::cout << i << " 2\n";
else
std::cout << i << " 2\n";
// if
int j = 1;
if (i > 1)
if (j > 2)
std::cout << i << " > 1 " << j << " > 2\n";
else // else if (j > 2), if (i > 1)
std::cout << i << " > 1 " << j << " <= 2\n";
// dynamic_cast
struct Base
{
virtual ~Base() {}
};
struct Derived : Base
{
void df() { std::cout << "df()\n"; }
};
Base* bp1 = new Base;
Base* bp2 = new Derived;
if (Derived* p = dynamic_cast<Derived*>(bp1)) // ,
// nullptr
p->df(); //
if (auto p = dynamic_cast<Derived*>(bp2)) //
p->df(); //
}
:
2 2
2 > 1 1 <= 2
df()
if-, if
, , ( ), , ( ), , . std::map<int, std::string> m;
std::mutex mx;
extern bool shared_flag; // mx
int demo()
{
if (auto it = m.find(10); it != m.end()) { return it->second.size(); }
if (char buf[10]; std::fgets(buf, 10, stdin)) { m[0] += buf; }
if (std::lock_guard lock(mx); shared_flag) { unsafe_ping(); shared_flag = false; }
if (int s; int count = ReadBytesWithSignal(&s)) { publish(count); raise(s); }
if (const auto keywords = {"if", "for", "while"};
std::ranges::any_of(keywords, [&tok](const char* kw) { return tok == kw; }))
{
std::cerr << " \n";
}
}
|
( C++17) | ||||||||||||||||||||||||||||||||||||||||||||||
Constexpr if, constexpr if return : template<typename T>
auto get_value(T t)
{
if constexpr (std::is_pointer_v<T>)
return *t; // int T = int*
else
return t; // int T = int
}
odr , extern int x; // x
int f()
{
if constexpr (true)
return 0;
else if (x)
return x;
else
return -x;
}
. void f()
{
if constexpr(false)
{
int i = 0;
int *p = i; // ,
}
}
template<typename T, typename ... Rest>
void g(T&& p, Rest&& ...rs)
{
// ... p
if constexpr (sizeof...(rs) > 0)
g(rs...); // .
}
. , , , template<class T>
void g()
{
auto lm = [=](auto p)
{
if constexpr (sizeof(T) == 1 && sizeof p == 1)
{
// g<T>,
// -
//
// -
}
};
}
: : template<typename T>
void f()
{
if constexpr (std::is_arithmetic_v<T>)
// ...
else
{
// : T
static_assert(false, " ");
using invalid_array = int[-1]; // : T
static_assert(false, " "); // CWG2518
}
}
CWG2518 catch-- , , template<typename>
inline constexpr bool dependent_false_v = false;
template<typename T>
void f()
{
if constexpr (std::is_arithmetic_v<T>)
// ...
else
{
// CWG2518
static_assert(dependent_false_v<T>, " "); // ok
}
}
(goto , : typedef ( C++23) constexpr if, .
|
( C++17) |
Consteval if,
- ( ) . , consteval if (, , ): constexpr void f(bool b)
{
if (true)
if consteval {}
else ; // :
// else if
}
#include <cmath>
#include <cstdint>
#include <cstring>
#include <iostream>
constexpr bool is_constant_evaluated() noexcept
{
if consteval { return true; } else { return false; }
}
constexpr bool is_runtime_evaluated() noexcept
{
if not consteval { return true; } else { return false; }
}
consteval std::uint64_t ipow_ct(std::uint64_t base, std::uint8_t exp)
{
if (!base) return base;
std::uint64_t res{1};
while (exp)
{
if (exp & 1) res *= base;
exp /= 2;
base *= base;
}
return res;
}
constexpr std::uint64_t ipow(std::uint64_t base, std::uint8_t exp)
{
if consteval //
{
return ipow_ct(base, exp);
}
else //
{
return std::pow(base, exp);
}
}
int main(int, const char* argv[])
{
static_assert(ipow(0,10) == 0 && ipow(2,10) == 1024);
std::cout << ipow(std::strlen(argv[0]), 3) << '\n';
}
|
( C++23) | ||||||||||||||||||||||||||||||||||||
- - , , :
if (x)
int i;
// i
if (x)
{
int i;
}
// i
, , , :
if (int x = f())
{
int x; // : x
}
else
{
int x; // : x
}
- goto longjmp, , - .
| ( C++17) ( C++23) |
| ( C++17) |
__cpp_if_constexpr |
201606L |
(C++17) | constexpr if
|
__cpp_if_consteval |
202106L |
(C++23) | consteval if
|
if, else, constexpr, consteval
C++:
| CWG 631 | C++98 | , |
( , C) |
(C++20) |
, () |
C
if | |