alignas(1) int a; //error structalignas(8) S {}; structalignas(1) U { S s; }; // error: alignment of U would have been 8 without alignas(1) 无效值不能用于对齐:如alignas(3);,若是0 ,则被忽略 Note: c++11才有的关键字: As of the ISO C11 standard, the C language has the _Alignas keyword and defines alignas as a preprocessor macro expanding to the keyword in the header <stdalign.h>, but in C++ this is a keyword, and the headers <stdalign.h> and <cstdalign> (until C++20) donot define such macro. They do, however, define the macro constant __alignas_is_defined. 例子: // every object of type struct_float will be aligned to alignof(float) boundary // (usually 4) structalignas(float) struct_float { // your definition here }; // every object of type sse_t will be aligned to 256-byte boundary structalignas(256) sse_t { float sse_data[4]; }; // the array "cacheline" will be aligned to 128-byte boundary alignas(128) char cacheline[128]; #include<iostream> intmain() { sse_t x, y, z; structdefault_aligned { float data[4]; } a, b, c; std::cout << "alignof(struct_float) = " << alignof(struct_float) << '\n' << "alignof(sse_t) = " << alignof(sse_t) << '\n' << "alignof(alignas(128) char[128]) = " << alignof(alignas(128) char[128]) << "\n\n"; std::cout << std::hex << std::showbase << "&x: " << &x << '\n' << "&y: " << &y << '\n' << "&z: " << &z << '\n' << "&a: " << &a << '\n' << "&b: " << &b << '\n' << "&c: " << &c << '\n'; } alignof(struct_float) = 4 alignof(sse_t) = 256 alignof(alignas(128) char[128]) = 128 &x: 0x7fffd901bb00 &y: 0x7fffd901bc00 &z: 0x7fffd901bd00 &a: 0x7fffd901bad0 &b: 0x7fffd901bae0 &c: 0x7fffd901baf0
#include<iostream> extern"C"intfunc(); // the definition of func is written in assembly language // raw string literal could be very useful asm(R"( .globl func .type func, @function func: .cfi_startproc movl $7, %eax ret .cfi_endproc )"); intmain() { int n = func(); // extended inline assembly asm ("leal (%0,%0,4),%0" : "=r" (n) : "0" (n)); std::cout << "7*5 = " << n << std::endl; // flush is intentional // standard inline assembly asm ("movq $60, %rax\n\t"// the exit syscall number on Linux "movq $2, %rdi\n\t"// this program returns 2 "syscall"); } out:7*5 = 35
const_cast < new_type > ( expression ) Returns a value of type new_type. 解释: 只有以下转换可以用const_cast实现,通常,only const_cast may be used to cast away (remove) constness or volatility. 1 同一个类型的两个可能是多级指针可以相互转换,而不管每个级别上的cv限定符是什么。 2 任何类型T的左值都可以转换为相同类型T的左值或右值引用,或多或少符合cv要求。同样,类类型的prvalue或任何类型的xvalue都可以转换为符合cv要求的右值引用。引用const_cast的结果指向原对象if表达式为glvalue,以及实体化临时表达式(自c++ 17起)。 3 同样的规则适用于可能指向数据成员的多级指针,也适用于可能指向已知和未知绑定的数组的多级指针(指向cv限定元素的数组本身被认为是cv限定的)(从c++ 17开始) 4 空指针值可以转换为new_type的空指针值 对于所有的cast表达式,结果可以是: 1) 如果new_type是左值引用类型或函数类型的右值引用,则为左值; 2)如果new_type是对象类型的右值引用,则为xvalue 3)一个prvalue ,对其他情况 note:指向函数和成员函数的指针不受const_cast约束 const_cast使实际引用const对象的非const类型的引用或指针,或者实际引用volatile对象的非volatile类型的引用或指针成为可能。通过非const访问路径修改const对象和通过非volatile glvalue引用volatile对象会导致未定义的行为
#include<iostream> structtype { int i; type(): i(3) {} voidf(int v)const{ // this->i = v; // compile error: this is a pointer to const const_cast<type*>(this)->i = v; // OK as long as the type object isn't const } }; intmain() { int i = 3; // i is not declared const constint& rci = i; const_cast<int&>(rci) = 4; // OK: modifies i std::cout << "i = " << i << '\n'; type t; // if this was const type t, then t.f(4) would be undefined behavior t.f(4); std::cout << "type::i = " << t.i << '\n'; constint j = 3; // j is declared const [[maybe_unused]] int* pj = const_cast<int*>(&j); // *pj = 4; // undefined behavior [[maybe_unused]] void (type::* pmf)(int) const = &type::f; // pointer to member function // const_cast<void(type::*)(int)>(pmf); // compile error: const_cast does // not work on function pointers } Output: i = 4 type::i = 4
int i = 4; int arr[5] = { 0 }; int *ptr = arr; structS{ double d; }s ; voidOverloaded(int); voidOverloaded(char);//重载的函数 int && RvalRef(); constboolFunc(int);