《现代c++语言核心特性解析》学习笔记(一)
1. long long 至少64位,%lld 和%llu 格式化符号, LL后缀#include `` #include ` ` #include ` ` int main(int argc, char *argv[]) { std::cout << "Hello world!" << std::endl; std::cout << "long long max: " << std::numeric_limits` `::max() << std::endl; std::cout << "long long min: " << std::numeric_limits` `::min() << std::endl; std::cout << "unsigned long long max: " << std::numeric_limits` `::max() << std::endl; std::printf("llong_max: %lldn", std::numeric_limits` `::max()); std::printf("llong_min: %lldn", std::numeric_limits` `::min()); std::printf("unsigned llong_max: %llun", std::numeric_limits` `::max()); }
Hello world! long long max: 9223372036854775807 long long min: -9223372036854775808 unsigned long long max: 18446744073709551615 llong_max: 9223372036854775807 llong_min: -9223372036854775808 unsigned llong_max: 18446744073709551615 Executing run: all complete!2. 内联命名空间和嵌套命名空间
#include3. auto特性#include #include namespace A::inline B::inline C { char foo() { return u8'c'; } } namespace A::inline B::D { char foo() { return u8'd'; } } // namespace A::inline B // 编译错误 // { // char foo() { // return u8'e'; // } // } namespace A::E { char foo() { return u8'e'; } } int main(int argc, char *argv[]) { std::cout << "Hello world!" << std::endl; auto a = A::foo(); std::cout << "auto type: " << a << std::endl; //c a = A::B::foo(); std::cout << "auto type: " << a << std::endl; //c a = A::B::D::foo(); std::cout << "auto type: " << a << std::endl; //d a = A::E::foo(); std::cout << "auto type: " << a << std::endl; //e }
推导规则,从左向右,往上推导;
auto i = true ? 5:8.0; std::cout << "auto type:" << typeid(i).name() << std::endl; // double
函数形参列表中可以使用,c++17后
void echo(auto str) {
std::cout << "echo auto type:" << typeid(str).name() << std::endl;
}
int main(int argc, char *argv[])
{
echo("string");
echo(10);
echo(10.0);
}
auto type:d echo auto type:PKc echo auto type:i echo auto type:d
各种推导规则:
int i = 5 ;
int &j = i;
auto &m = j;
i++;
std::cout << i << j << m << std::endl;
auto&& n = i; //int &
i++;
auto&& k = 5; //int &&
std::cout << i << n << k << std::endl;
auto i1 = {1, 2};
auto i2{1};
auto i3 = {1};
auto i4 = [](){;};
std::cout << "auto type: " << typeid(i1).name() << std::endl;
std::cout << "auto type: " << typeid(i2).name() << std::endl;
std::cout << "auto type: " << typeid(i3).name() << std::endl;
std::cout << "auto type: " << typeid(i4).name() << std::endl;
666 775 auto type: St16initializer_listIiE auto type: i auto type: St16initializer_listIiE auto type: Z4mainEUlvE_
类继承时推导
class Base {
public:
virtual void foo() {
std::cout << "call base foo:" << std::endl;
}
};
class Derived: public Base {
public:
virtual void foo() override {
std::cout << "call derivced foo:" << std::endl;
}
};
int main(int argc, char *argv[])
{
Base *b = new Derived();
b->foo();
auto e = b;
e->foo();
auto &f = b;
f->foo();
auto d = *b;
d.foo();
auto &c = *b;
c.foo();
auto *h = &d;
h->foo();
auto *m = &(*b);
m->foo();
auto n = *&b;
n->foo();
}
call derivced foo: call derivced foo: call derivced foo: call base foo: call derivced foo: call base foo: call derivced foo: call derivced foo:
lambda的后置返回类型推导
auto lm = [](int &i)->auto& {return i;};
auto x1 = 5;
auto &x2 = lm(x1);
assert(&x1 == &x2); // 相同内存地址
非类型模板形参占位符
f<5>(); f<'5'>(); f(); //f<5.0>(); //f<{1,2}>(); //f ();
auto type:i auto type:c auto type:b



