[ Web Proxy ]
URL:
Viewing: https://ru.cppreference.com/cpp/language/if#Consteval_if [Back]  [Original]

if cppreference.com
cppreference.com

if

cppreference.com

.

, ( 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)
1) if else
2) if else
3) consteval if else
4) consteval if else
( C++11)
constexpr ( C++17) , constexpr if
- ( C++17)
( C++23)
, - ;, , .
  • bool
  • , , .
- ( ), , true
- ( ), , false
, , if
  • , ! consteval
  • , ! consteval
( , ), , if
  • , ! consteval
  • , ! consteval

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

{
-
 () if constexpr() ( )
-

}

{
-
 () if constexpr() ( )
-
else
-

}

, , ( ), , ( ), , .

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

, if constexpr constexpr if.

constexpr if bool ( C++23), bool, ( C++23). true, - ( ), -.

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

. if constexpr #if:

void f()
{
    if constexpr(false)
    {
        int i = 0;
        int *p = i; // ,     
    }
}

constexpr if , , .

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-- , , false:

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 , case default:), constexpr, ( switch goto) .

: typedef ( C++23) constexpr if, .

( C++17)

Consteval if

, if consteval , consteval if. consteval if,

 () if !() consteval - (3) ( C++23)
 () if !() consteval - else (4) ( C++23)

- ( ) .

, consteval if (, , ):

constexpr void f(bool b)
{
    if (true)
        if consteval {}
        else ; // :   
               // else     if
}

consteval if , -. , , .

case default , consteval if, switch if. , consteval if, .

if !consteval, - ( ) . consteval if, consteval if:

  • if !consteval {/*stmt*/} if consteval {} else {/*stmt*/}.
  • if !consteval {/*stmt-1*/} else {/*stmt-2*/} if consteval {/*stmt-2*/} else {/*stmt-1*/}.

- consteval 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, , - .

constexpr if, - bool.

( C++17)
( C++23)

switch goto constexpr if consteval if ( 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 if

Web Proxy Viewer  |  New URL  |  Original Page