"Just for fun"
很明顯我不是這種人
我開了很多坑,但常常不補坑
I love Magic
工程師或許就是魔法師
我們甚至有本課本叫魔法書
還有黑魂
哪天多一本 Elden ring 都不奇怪
ios_base::sync_with_stdio(0), cin.tie(0);
The standard of C++
S
instance in each part will be constructed?S fn() { return S(); }
int main() {
// S is a struct; Cpp version: C++17
S s1 = fn();
S s2 = S( S( fn() ) );
S s3{ S( fn() ) };
S s4;
s4 = S( fn() );
}
S
instance in each part will be constructed?S fn1() {
S obj;
return obj;
}
S fn2(bool flag) {
if (flag) {
S obj1;
return obj1;
}
else {
S obj2;
return obj2;
}
}
int main(int argc, char *argv[]) {
S s1 = fn1();
S s2 = fn2(argc == 1);
}
int main()
{
std::string str = "Mes";
std::string str2 = "A very very long string";
}
_CONSTEXPR20 basic_string& assign(
_In_reads_(_Count) const _Elem* const _Ptr, _CRT_GUARDOVERFLOW const size_type _Count) {
// assign [_Ptr, _Ptr + _Count)
if (_Count <= _Mypair._Myval2._Myres) {
_Elem* const _Old_ptr = _Mypair._Myval2._Myptr();
_Mypair._Myval2._Mysize = _Count;
_Traits::move(_Old_ptr, _Ptr, _Count);
_Traits::assign(_Old_ptr[_Count], _Elem());
return *this;
}
return _Reallocate_for(
_Count,
[](_Elem* const _New_ptr, const size_type _Count, const _Elem* const _Ptr) {
_Traits::copy(_New_ptr, _Ptr, _Count);
_Traits::assign(_New_ptr[_Count], _Elem());
},
_Ptr);
}
class T {
public:
T() { puts("T()"); }
~T() { puts("~T()"); }
};
T()
就是一種建構子,而 ~T()
就是一種解構子。#include <iostream>
struct T {
T() { puts("T()"); }
~T() { puts("~T()"); }
};
int main()
{
{
T t;
} // use RAII to destruct t
}
template<typename First, typename Second>
struct T{
First i;
Second j;
};
#include <algorithm>
#include <vector>
int main()
{
std::vector<int> vec = { 3, 1, 2 };
std::sort(vec.begin(), vec.end()); // The elements of vec will be 1 2 3.
}
#include <algorithm>
#include <vector>
void count_things(const std::vector<int> &vec, int value)
{
const auto count =
std::count(std::begin(vec), std::end(vec), value);
}
#include <vector>
void travel_thing(const std::vector<int> &vec)
{
for (const auto &elem : vec) {
// do thing with elem
}
}
#include <algorithm>
#include <vector>
template <typename T>
void count_things_less_than_3(const T &vec)
{
const auto count = std::count_if(std::begin(vec), std::end(vec),
[](int i) { return i < 3; }
);
}
template <typename Func, typename... T>
void caller(const Func &func, const T &...param)
{
func(param...);
}
#include <memory>
void allocate_memory()
{
std::unique_ptr<int> ptr(new int(5));
} // ptr destory, memory free
make_unique
等來分配智慧指標的空間#include <memory>
void allocate_memory()
{
auto ptr{std::make_unique<int>(5)};
}
constexpr int get_value()
{
return 5 * 3;
}
constexpr auto value = get_value();
#include <iostream>
class T {
public:
T() { puts("T()"); }
~T() { puts("~T()"); }
};
T fn()
{
return T();
}
int main()
{
T t = fn();
}
#include <string_view>
constexpr std::string_view name = "Hello";
#include <array>
std::array data{1, 2, 3, 4, 5};
template<typename ...T>
auto add(const T & param...)
{
return (param + ...);
}
std::pair<int, int> values{1, 2};
auto [first, second] = values;
void fn(int i, int t){
if(int j = i * t;
j < 5)
{
// do something
}
}
Value Categories is a Property of
很重要所以講三次,
Expression、Expression、Expression
42 // Expression evaluating to value 42
17 + 42 // Expression evaluating to value 59
int a;
a = 23 // Expression evaluating to value 23
a + 17 // Expression evaluation to value 40
static_cast<float>(a) // Expression evaluating to
// floating-point value 23.0f
全名為 identity,你可以簡單理解為有記憶體位址
int a;
a // 擁有 identity
a + 2 // 沒有identity
a || true // 沒有identity
a++ // 沒有identity
++a // 擁有identity
42 // 沒有 identity
nullptr // 沒有 identity
false // 沒有 identity
[]{return 42;} // 沒有 identity
"Hello world" // 擁有 identity!!
std::cout // 擁有 identity,std::cout 是 std::ostream 的 instance
static_cast<int>(a) // 沒有 identity
std::move(a) // 擁有 identity
代表其資源可以被安全的轉移給其他人
資源通常指的是記憶體、socket 等等
原先 obj 由 ptrA 控管,經移動後由 ptrB 控管
#include <iostream>
#include <vector>
std::string func() {
return "Steal me!";
}
int main() {
std::vector<std::string> vec;
vec.push_back( func() );
std::string x{ "Steal me!" };
vec.push_back( std::move( x ) );
return 0;
}
42 // prvalue
nullptr // prvalue
int foo() {
return 0;
}
foo(); // foo() is prvalue
int x = 42;
x++ // prvalue
"Hello world" // lvalue
int x = 42;
++x // lvalue
x // lvalue
struct S {
int i;
};
S().i // xvalue
int i;
std::move(i); // xvalue
namespace detail {
template <class T> struct value_category {
static constexpr char const * value = "prvalue"; };
template <class T> struct value_category<T&> {
static constexpr char const * value = "lvalue"; };
template <class T> struct value_category<T&&> {
static constexpr char const * value = "xvalue"; };
}
(7.6.2):The operand of the unary & operator shall be an lvalue of some type T. The result is a prvalue.
能用 & 的 Expression 為 lvalue Expression
S fn() { return S(); }
int main() {
// S is a struct; Cpp version: C++17
S s1 = fn();
S s2 = S( S( fn() ) );
S s3{ S( fn() ) };
S s4;
s4 = S( fn() );
}
查表吧🈹(link)
那叫 NRVO,有興趣的可以去看看