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

cppreference.com
cppreference.com

cppreference.com
 
C++
(C++20)
(C++20)
(C++11)
(C++20)
/
(C++11)
(C++11)
(C++11)
(C++17)
 
 
(lvalue, rvalue, xvalue)
( )
-(C++11)
,
(C++11)
(C++11)
:
:  
:
:
:
:
:
a=b, a+=b, a-=b, a*=b, a/=b, a%=b, a&=b, a|=b, a^=b, a<<=b, a>>=b
++a, --a, a++, a--
+a, -a, a+b, a-b, a*b, a/b, a%b, ~a, a&b, a|b, a^b, a<<b, a>>b
a||b, a&&b, !a
a==b, a!=b, a<b, a>b, a<=b, a>=b, a<=>b ( C++20)
a[b], *a, &a, a->b, a.b, a->*b, a.*b
a(...), a,b, a?b:c
new
delete
throw
alignof
sizeof
sizeof...(C++11)
typeid
noexcept(C++11)
(C++17)
(C++20)
const_cast
static_cast
reinterpret_cast
dynamic_cast
: (T)a, T(a), auto(a), auto{a} ( C++23)
 

, .

( - ) (1)
- ( - () ) (2)
- { - () } (3) ( C++11)
- ( - () ) (4) ( C++17)
- { - () } (5) ( C++17)
auto ( ) (6) ( C++23)
auto { } (7) ( C++23)

-.

1) C :
a) const_cast<->();
b) static_cast<->(), : ( ), ( ). ;
c) static_cast ( ), const_cast;
e) reinterpret_cast const_cast.
, , ( ). static_cast, const_cast, .
, C , . - , , static_cast reinterpret_cast.
2) typedef ( , , , unsigned int() int*() ), .
  • , C.
  • ( C++11) , - . prvalue - ( C++17) ( C++17) -.
  • : - , , prvalue -, ( C++17) (, cv-) ( C++17) . - , . - (, cv-) void, void prvalue ( C++17).
3) , , , prvalue , ( C++17) ( C++17), . - (, cv-) void, void prvalue ( C++17). , prvalue. ( C++20)
4,5) , (2,3), , .
6,7) auto x, auto x(); ( ) auto x{}; . prvalue .

, :

  • lvalue, - lvalue rvalue ( C++11);
  • xvalue, - rvalue ;
( C++11)
  • prvalue.

. : , , , :

struct M {};
struct L { L(M&); };
 
M n;
void f()
{
    M(m);    // ,  M m;
    L(n);    //   ,  L n;
    L(l)(m); //   ,  L l((m));
}

, , auto:

	
struct M;

struct S
{
    S* operator()();
    int N;
    int M;

    void mem(S s)
    {
        auto(s)()->M; //  (S::M  ::M),   C++23
    }
};

void f(S s)
{
    {
        auto(s)()->N; // ,   C++23
        auto(s)()->M; //  ,  M s();
    }
    {
        S(s)()->N;    // 
        S(s)()->M;    // 
    }
}
( C++11)

. . , , :

struct S
{
    S(int);
};

void foo(double a)
{
    S w(int(a)); //  :   `a`  int
    S x(int());  //  :     int(*)(),
                 //    int()

    //   :
    S y((int(a))); //  :   
    S y((int)a);   //  :    C
    S z = int(a);  //  :     
}

, , auto:

typedef struct BB { int C[2]; } *B, C;

void foo()
{
    S a(B()->C);    //  : B()->C    
    S b(auto()->C); //  :     C(*)(),
                    //     C()
}
( C++11)

- . , , , :

// `int()`  `int(unsigned(a))`      :
// `int()`             ,  int
//                       
// `int(unsigned(a))`  ,  int
//                        unsigned
void foo(signed char a)
{
    sizeof(int());            //   ( )
    sizeof(int(a));           // 
    sizeof(int(unsigned(a))); //   ( )
    
    (int()) + 1;            //   ( )
    (int(a)) + 1;           // 
    (int(unsigned(a))) + 1; //   ( )
}

, , auto:

typedef struct BB { int C[2]; } *B, C;

void foo()
{
    sizeof(B()->C[1]);    // OK, sizeof()
    sizeof(auto()->C[1]); // : sizeof ,  
}
( C++11)

__cpp_auto_cast 202110L (C++23) auto(x) auto{x}

#include <cassert>
#include <iostream>

double f = 3.14;
unsigned int n1 = (unsigned int)f; //    C
unsigned int n2 = unsigned(f);     //    

class C1;
class C2;
C2* foo(C1* p)
{
    return (C2*)p; //      
}

void cpp23_decay_copy_demo()
{
    auto inc_print = [](int& x, const int& y)
    {
        ++x;
        std::cout << "x:" << x << ", y:" << y << '\n';
    };

    int p{1};
    inc_print(p, p); //  x:2 y:2,    y  
                     //  p
    int q{1};
    inc_print(q, auto{q}); //  x:2 y:1, auto{q} (C++23)   prvalue,
                           //   y   q (   q)
}

//       C  
// static_cast,       reinterpret_cast
struct A {};
struct I1 : A {};
struct I2 : A {};
struct D : I1, I2 {};

int main()
{
    D* d = nullptr;
//  A* a = (A*)d;                   //   
    A* a = reinterpret_cast<A*>(d); //  
    assert(a == nullptr);
    
    cpp23_decay_copy_demo();
}

:

x:2 y:2
x:2 y:1

C++:

CWG 1223 C++11
CWG 2620 C++98

  • C++23 (ISO/IEC 14882:2023):
  • 7.6.1.4 ( ) [expr.type.conv]
  • 7.6.3 ( ) [expr.cast]
  • C++20 (ISO/IEC 14882:2020):
  • 7.6.1.4 ( ) [expr.type.conv]
  • 7.6.3 ( ) [expr.cast]
  • C++17 (ISO/IEC 14882:2017):
  • 8.2.3 ( ) [expr.type.conv]
  • 8.4 ( ) [expr.cast]
  • C++14 (ISO/IEC 14882:2014):
  • 5.2.3 ( ) [expr.type.conv]
  • 5.4 ( ) [expr.cast]
  • C++11 (ISO/IEC 14882:2011):
  • 5.2.3 ( ) [expr.type.conv]
  • 5.4 ( ) [expr.cast]
  • C++03 (ISO/IEC 14882:2003):
  • 5.2.3 ( ) [expr.type.conv]
  • 5.4 ( ) [expr.cast]
  • C++98 (ISO/IEC 14882:1998):
  • 5.2.3 ( ) [expr.type.conv]
  • 5.4 ( ) [expr.cast]

const_cast const[]
static_cast []
dynamic_cast []
reinterpret_cast []
[]

Web Proxy Viewer  |  New URL  |  Original Page