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

constexpr ( C++11) cppreference.com
cppreference.com

constexpr ( C++11)

cppreference.com
  • constexpr - ,

constexpr , . , ( , ).

constexpr, - ( C++14), const. constexpr, ( C++17) inline. - constexpr, .

constexpr

constexpr :

  • , .. :
  • (, ) ,
  • (, ) , constexpr, e , e , , mutable ( mutable) e.
( C++20)

constexpr ---, , (, ) , --- , . ( --, ) .

( C++20)

constexpr

constexpr :

( C++20)
( C++20)
  • ( C++20),
( C++23)
  • , , :
( C++14)
  • :
  • goto
  • , case default
( C++20)
( , =default; =delete;, .)
( C++14)
( C++23)

constexpr

constexpr, =delete;, :

( C++20)
  • , , constexpr.

constexpr

constexpr, .

( C++20)

constexpr, =delete;, :

  • , , constexpr .
( C++20)
( C++23)

constexpr constexpr - . - constexpr, . constexpr, , , , . ( C++23)

noexcept true , , constexpr :

constexpr int f(); 
constexpr bool b1 = noexcept(f()); // false,  constexpr 
constexpr int f() { return 0; }
constexpr bool b2 = noexcept(f()); // true, f()   
( C++17)

constexpr , . , std::unique_ptr constexpr, .

constexpr ( ):

static constexpr int const& x = 42; // constexpr    const int
                                    // (    
                                    //  -     )

, try constexpr, - .

, , , .

( C++20)
__cpp_constexpr 200704L (C++11) constexpr
201304L (C++14) constexpr, -const constexpr
201603L (C++17) Constexpr
201907L (C++20) asm constexpr
202002L (C++20)
202110L (C++23) , goto constexpr
202207L (C++23) constexpr
202211L (C++23) static constexpr constexpr
202306L (C++26) Constexpr void*: constexpr
__cpp_constexpr_in_decltype 201711L (C++11)
(DR)
,
__cpp_constexpr_dynamic_alloc 201907L (C++20) constexpr

constexpr

constexpr C++11/14, ; , :

#include <iostream>
#include <stdexcept>

//  constexpr ++11  ,   
constexpr int factorial(int n)
{
    return n <= 1 ? 1 : (n * factorial(n - 1));
}

//  constexpr ++14      
#if __cplusplus >= 201402L
constexpr int factorial_cxx14(int n)
{
    int res = 1;
    while (n > 1)
        res *= n--;
    return res;
}
#endif // C++14

//  
class conststr
{
    const char* p;
    std::size_t sz;
public:
    template<std::size_t N>
    constexpr conststr(const char(&a)[N]): p(a), sz(N - 1) {}
    
    //  constexpr   ,  
    //  C++11,         ?:
    constexpr char operator[](std::size_t n) const
    {
        return n < sz ? p[n] : throw std::out_of_range("");
    }
    
    constexpr std::size_t size() const { return sz; }
};

//  constexpr ++11        return
// (++14    )
constexpr std::size_t countlower(conststr s, std::size_t n = 0,
                                             std::size_t c = 0)
{
    return n == s.size() ? c :
        'a' <= s[n] && s[n] <= 'z' ? countlower(s, n + 1, c + 1) :
                                     countlower(s, n + 1, c);
}

//  ,      
template<int n>
struct constN
{
    constN() { std::cout << n << '\n'; }
};

int main()
{
    std::cout << "4! = " ;
    constN<factorial(4)> out1; //    

    volatile int k = 8; //    volatile
    std::cout << k << "! = " << factorial(k) << '\n'; //    

    std::cout << "    \", !\"  ";
    constN<countlower(", !")> out2; //    conststr

    constexpr int a[12] = {0, 1, 2, 3, 4, 5, 6, 7, 8};
    constexpr int length_a = sizeof(a)/sizeof(int); // std::size(a)  C++17,
                                                    // std::ssize(a)  C++20
    std::cout << "  " << length_a << "  : ";
    for (int i = 0; i < length_a; ++i)
        std::cout << a[i] << " ";
}

:

4! = 24
8! = 40320
    \", !\"  8
  12  : 0 1 2 3 4 5 6 7 8 0 0 0

C++:

CWG 1712 C++14 constexpr ,
constexpr ( ,

constexpr)
CWG 1911 c++11 constexpr
CWG 2004 c++11 / mutable
mutable
/
CWG 2163 c++14 constexpr,
CWG 2268 c++11 / mutable
CWG 2004
,

,
consteval (C++20) , , []
constinit (C++20) , , []
C constexpr

Web Proxy Viewer  |  New URL  |  Original Page