#include <iostream>
#include <limits>
#include <future>
#include <string>
#include <map>
using namespace std;
template<auto N> //模板参数支持auto
class VarType {
public:
void Print() {
std::cout << N << std::endl;
}
};
template<typename T1, typename T2>
auto add(T1 a, T2 b) {
return a + b;
}
struct Data {
double d = 1.0;
};
template<typename T>
decltype(auto) return_ref(T &v) { //decltype(auto)可以返回引用
return v;
}
template<typename T>
std::string GetXX(T &&v) {
std::string vv = static_cast<T&&>(v);
std::cout <<"v="<<v << " vv is : " << vv << std::endl;
return vv;
}
class Work {
private:
int value;
public:
Work():value(42){}
std::future<int> spawn() {
return std::async([=, *this]{return value * 10;}); //*this把对象 copy了
}
};
std::future<int> foo() {
Work tmp;
return tmp.spawn();
}
union U {
U(){}
~U(){}
int a;
double d;
std::string c; //联合里可以有对象了,但是必须要有构造和析构函数
};
int main() {
std::cout << (65536 << 16) << std::endl;
std::cout << (65536LL << 16) << std::endl;
std::cout << std::numeric_limits<unsigned long long>::max() << std::endl;
char utf8 = u8'c';
char16_t utf16 = u'你';
char32_t utf32 = U'好';
std::cout << utf8 << utf16 << utf32 << std::endl;
auto f = [](auto a, auto b)->auto { //auto指定参数类型和返值类型
return a + b;
};
auto f2 = []<typename T1, typename T2>(T1 const &a, T2 const &b)->auto { //lambda支持模板参数
T1 aa;
T2 bb;
return a + b;
};
auto greater = [](auto a, auto b)->auto {
return a > b;
};
std::map<std::string, decltype(greater)> mmm; //labmda函数可以放进map了
std::cout << "a+b=" << f(2,3) << std::endl;
std::cout << "a+b=" << f2(2,4) << std::endl;
VarType<'Y'>().Print();
VarType<222>().Print();
const int const_a = 1111;
auto b = const_a;
b = 333;
auto &const_b = const_a;
//const_b = 222; //const cannot assign
//auto &right_val_b = 3; //fail, 3 is right vale, cannot refrenece
auto &&right_val_b = 4;
right_val_b = 4;
std::cout << add(3, 4.5) << std::endl;
const Data v;
decltype(v.d) vv = v.d;
vv = 333; //vv is double. not double &
decltype((v.d)) vvv = v.d; //(v.d)和v.d是不同的
//vvv = 444; /// vvv is const double
std::cout << "v = " << v.d << std::endl;
int x =345;
int &x_ref = return_ref(x);
x_ref = 456;
std::cout << x << std::endl;
//右值引用
int &&a = 333; //本来333是临时变量,但是右值引用延长了临时对象的生命周期
a = 44; //还能修改
const int &aa = 333; //同样延长了对象的生命周期,但是不能修改
//int &&cv = x; //右值不能引用左值
std::string s1 = "hello";
std::string &&s2 = std::move(s1); //表示s1里的资源可以被随时挪走,此后s1应该废了。不能再假设s1的值是hello了,
std::cout << "s1:" << s1 << std::endl; //s2是引用,所以没有把s1的值偷走
std::string s3 = std::move(s1); //表示s1里的资源可以被随时挪走,此后s1应该废了。不能再假设s1的值是hello了,
std::cout << "s1:" << s1 << std::endl;//s3是值,所以有把s1的值偷走
std::string s4 = GetXX(s3);
std::cout << "s3: " << s3 << " s4: " << s4 << std::endl;
std::string s5 = GetXX(std::move(s3));
std::cout << "s3: " << s3 << " s5: " << s5 << std::endl;
auto ff = foo(); //future
std::cout << "f.get: " << ff.get() << std::endl;
//联合
U u;
new(&u.c) std::string;
u.c="hello";
u.c.~basic_string();
u.a = 111;
std::map<std::string, int> ms{{"aa",1}, {"bb",2}, {"cc",2}};
for(auto &[k,v] : ms) { //KV可以解包了
std::cout << k << " " << v << std::endl;
}
//std::cout << "san: " << ((7<=>8) < 0) << std::endl;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)