1, Keywords
1.1 alignas
- func: the specifier can be applied to the declaration of variables, non bit field class data members, class/struct/union and enumeration definitions. Cannot be applied to parameters;
- Note: but it is weaker than the original alignment;
struct alignas(8) S {};
struct alignas(1) U { S s; }; // Error: if there is no align (1), the alignment of U will be 8
1.2 alignof
- func: get memory alignment instead__ alignof__; Can be used for full type, array type or reference type;
#include <iostream>
struct Foo {
int i;
float f;
char c;
};
struct Empty {};
struct alignas(64) Empty64 {};
int main()
{
std::cout << "Alignment of" "\n"
"- char : " << alignof(char) << "\n"
"- pointer : " << alignof(int*) << "\n"
"- class Foo : " << alignof(Foo) << "\n"
"- empty class : " << alignof(Empty) << "\n"
"- alignas(64) Empty: " << alignof(Empty64) << "\n";
}
1.3 decltype
- func: type derivation of the parameter during compilation, and only the type of the parameter is returned;
- Combined with the return value of auto tracking function;
Auto multiply (_txx, _tyy) [function name] - [decltype (_tx * _ty) - Reuse anonymous structures; Decltype (structure name) the new name of the structure
- Type can be defined in combination with typedef/using;
using size_t = decltype(sizeof(0))
Typedef decltype (variable) type name
1.4 auto
- func: automatic type judgment of variables;
- It is generally used to replace the long and complex variable declaration with specific variable use range;
int a = 10;
auto au_a = a; //Automatic type inference, au_a is of type int
cout << typeid(au_a).name() << endl;
1.5 static_assert
- func: static assertion - compiler stage assertion; The assert contacted before is the runtime assertion;
static_assert(Constant expression,"Prompt language");
1.6 noexcept
- func: check at compile time. If the expression is declared not to throw any exception, it will return true;
void func() noexcept;
1.7 constexpr
- func: constexpr specifier declares that the value of a function or variable can be obtained at compile time;
- Requirements: its type must be literal, it must be initialized immediately, and the complete initialization expression, including all implicit conversions, constructor calls, etc., must be a constant expression;
int i;
const int size = i;
int arr[size]; //error, size is not a constant expression and cannot be determined at compile time
constexpr auto size = 10;
int arr[size]; //OK, constant expression for size
int i;
constexpr int size = i; // error,i cannot be determined at compile time
constexpr int foo(int i) {
return i + 5;
}
int main() {
int i = 10;
std::array<int, foo(5)> arr; // OK, 5 is a constant expression, and foo(5) calculated is also a constant expression
foo(i); // Call is Ok, i is not a constant expression, but it can still be called (constexpr is ignored)
std::array<int, foo(i)> arr1; // Error, but the call result of foo(i) is not a constant expression
}
1.8 thread_local
- func: it can create a local copy of a global variable or object in a thread, which can avoid resource competition in the case of multiple threads;
Thread storage period. Object is allocated at the beginning of the thread and deallocated at the end of the thread. Each thread has its own instance of the object. Only declared as thread_ The object of local has this storage period. It can appear together with static and extern.
static thread_local int thread_count = 1;
2, Predefined macro
2.1 __STDC_HOSTED__
- func: the target system environment of the compiler, including the standard C library. The macro definition is 1, otherwise it is 0;
2.2 __STDC__
- func: in the C compiler, it indicates whether the compiler is consistent with the C standard. In the C + + compiler, the compiler determines whether the macro is defined and what value it is defined;
2.3 __STDC__VERSION__
- func: in the C compiler, it indicates the Standard Version supported by the compiler. In the C + + compiler, the compiler determines whether this macro is defined and what value it is defined;
2.4 __STDC_ISO_10646__
- func: used to indicate that the C + + compilation environment conforms to a version of ISO/IEC 10646 standard;
2.5 __func__
- func: returns the name of the function in which it is located;
const char *hello() { return __func__; }
2.6 _Pragma
- func: _ Pragma (string literal) = = #pragma once;
2.7 __VA_ARGS__
- func: you can replace the string represented by the ellipsis in the implementation part of the macro definition;
#define PR(...) printf{__VA_ARGS__}
3, Class correlation
3.1 final
- func: prohibit subclasses from overriding functions of the parent class;
void func() final
3.2 override
- func: describes that the subclass overrides the parent function;
void func() override;
3.3 inheritance constructor
- Constructor: func direct initializable parent class;
#include <string>
class A
{
public:
A(int i){};
A(std::string str){};
};
class B : A
{
public:
using A::A;
};
int main(int argc, char const *argv[])
{
B b1(123); // ok
B b2("abc"); // ok
return 0;
}
4, Other
4.1 lambda
- Format: [capture list] (params list) mutable exception - > return type {function body};
- capture list: capture the list of external variables;
- params list: parameter list;
- mutable indicator: used to say whether the captured variable can be modified;
- Exception: exception setting;
- Return type: return type;
- Function body: function body;
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(int a, int b)
{
return a < b;
}
int main()
{
vector<int> myvec{ 3, 2, 5, 7, 3, 2 };
vector<int> lbvec(myvec);
sort(myvec.begin(), myvec.end(), cmp); // Old practice
cout << "predicate function:" << endl;
for (int it : myvec)
cout << it << ' ';
cout << endl;
// Lambda expression
sort(lbvec.begin(), lbvec.end(), [](int a, int b) -> bool { return a < b; });
cout << "lambda expression:" << endl;
for (int it : lbvec)
cout << it << ' ';
}