()=delete;};#if __cpp_static_assert >= 202306L// C++ 目前还不能真正使下面这句工作(需要 std::format 为 constexpr):static_assert(sizeof(long)==4,std::format("期待 4,得到 {}", sizeof(long)));#endifintmain(){inta, b;swap(a, b);no_copy nc_a, nc_b;swap(nc_a, nc_b);...
structX{staticconstintn=1;staticconstexprintm=4;};constint*p=&X::n,*q=&X::m;// 因为X::n 与 X::m 被 ODR 式使用constintX::n;// ……所以需要定义constexprintX::m;// ……(但 C++17 中的 X::m 不需要) 关键词 static
This convenience macro expands to the keyword_Static_assert. Example Run this code #include <assert.h>intmain(void){static_assert(2+2==4,"2+2 isn't 4");// well-formedstatic_assert(sizeof(int)<sizeof(char),// compile-time error"this program requires that int is less than char");...
structX{staticvoidf();// declarationstaticintn;// declaration};X g(){returnX();}// some function returning Xvoidf(){X::f();// X::f is a qualified name of static member functiong().f();// g().f is member access expression referring to a static member function}intX::n=7;...
T1,T2);static_assert(std::is_same_v<decltype((bar<char>)(0,0)),int>);static_assert(std:...
The storage of the vector is handled automatically, being expanded as needed. Vectors usually occupy more space than static arrays, because more memory is allocated to handle future growth. This way a vector does not need to reallocate each time an element is inserted, but only when the additi...
intmain() {union{inta;constchar*p; }; a=1; p="Jennifer"; } union-like class #include<iostream>//S has one non-static data member (tag), three enumerator members,//and three variant members (c, n, d)structS {enum{CHAR, INT, DOUBLE} tag;union{charc;intn;doubled; ...
a cast expression to rvalue reference to function type, such as static_cast<void (&&)(int)>(x). (C++ 11 右值强转函数类型) Properties: Same as glvalue (below). Address of an lvalue may be taken: &++i[1] and &std::endl are valid expressions. ...
int x = static_cast<int>(President::MCKINLEY); // can specify any integer data type as the underlying type enum class Day : char {MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY}; 6. Templates 6.1. Function Templates template <class T> void swap(T &i, T &j) { T temp = i; i = ...
#includeusing namespace std; templateT add(T lhs, T rhs) { return lhs + rhs; } int main() { cout << "1 + 2 =" << add(1, 2) << endl; cout << "1.0 + 2.0 = " << add(1.0, static_cast(2)) << endl; cout << "1.0 + 2.0 = " << add(1.0, 2) << endl; } ...