线路代号30的表示方法(C17in)

anon类模板参数推导,今天小编就来聊一聊关于线路代号30的表示方法?接下来我们就一起去研究一下吧!

线路代号30的表示方法(C17in)

线路代号30的表示方法

anon

类模板参数推导

std::pair p(2, 4.5); // std::pair<int, double> std::tuple t(4, 3, 2.5); // std::tuple<int, int, double>

inline variable

// In a.hpp inline SomeType singletonVar;

每个翻译单元都要include a.hpp,最后只会保留一个

细化Evaluation order

a[i ] = i; // 等号右边的现在规定会先计算完成 /* a.b a->b a->*b a(b1, b2, b3) b @= a a[b] a << b a >> b 现在都会a, b, c, d顺序计算 */

guaranteed copy elision

std::mutex a_mutex; std::lock_guard<std::mutex> get_lock { return std::lock_guard<std::mutex>(a_mutex); // lock_guard没有复制、移动构造函数。17之前这句不合法(尽管事实上都能编译过)。17之后auto lock = get_lock;就合法了 }

if statement with initilizer

if (auto it = m.find(10); it != m.end) // return *it

structured binding

std::map<int, int> m; auto [iter, ok] = m.insert({1, 2}); // insert返回pair<Iterator, bool>

string_view

std::string cppstr = "Foo"; std::string_view cppstr_v(&cppstr[0], cppstr.size); cppstr[0] = 'A'; std::cout << cppstr_v; // Aoo

嵌套namespace

namespace a::b { // ... }

if constexpr

template<typename ... Ts> void f(Ts ... args) { if constexpr(sizeof... (args) > 0) f1(args..); else f1(default_arg); }

Fold 表达式

template<typename ... Ts> constexpr int sum(Ts... args) { return ( 0 ... args ); // = ( (0 arg0) arg1 ) arg2 ... } static_assert(10 == sum(2, 3, 5)); // static_assert可以没有信息了

模板非类型参数auto

template<auto C> struct Size { static const size_t size = sizeof(C); }; static_assert(Size<3>::size == sizeof(3)); static_assert(Size<(void*)0>::size == sizeof(void*));

lambda改进

struct Foo { int val; void f { (*this) { val = 100; }; // 复制了整个对象进lambda } }; Foo obj {42}; obj.f; assert(obj.val == 42); auto getSize = (auto o) { return sizeof(o); } // 这是constexpr了 static_assert(getSize(obj) == sizeof(Foo));

void_t,更方便的SFINAE

template< class, class = std::void_t<> > struct has_type_member : std::false_type { }; template< class T > struct has_type_member<T, std::void_t<typename T::type>> : std::true_type { };

{variant/optional/any}

optional<const char*> get_foo { if (...) return some_msg; else return {}; } std::cout << get_foo.value_or("Not found!");

variant<int, float> v, w; v = 12; int i = get<int>(v); w = get<int>(v); w = get<0>(v); // 同上 w = v; // 同上 get<double>(v); // ill formed

<algorithm>改进

std::sort(std::execution::par, std::begin(v), std::end(v)); // 并行版 std::vector<int> v2{ 1, 2, 4, 5, 0 }; assert(12 == std::reduce(std::execution::par, std::begin(v2), std::end(v2))); // 乱序版accumulate

filesystem

std::filesystem::copy("/etc/resolv.conf", "./resolv.conf", std::filesystem::copy_options::overwrite_existing); for (auto it = std::filesystem::recursive_directory_iterator("/usr/bin"); it != std::filesystem::recursive_directory_iterator; it) { std::cout << it->path << std::endl; }

Aligned dynamic allocation

namespace std { enum class align_val_t: size_t; }; void *operator new(std::size_t, std::align_val_t);// new overload 分配一段对齐的内存

在不同容器间移动结点

map<int, string> src {{1,"one"}, {2,"two"}, {3,"buckle my shoe"}}; map<int, string> dst {{3,"three"}}; dst.insert( src.extract( src.find(1) ) ); dst.insert( src.extract(2) );

,

免责声明:本文仅代表文章作者的个人观点,与本站无关。其原创性、真实性以及文中陈述文字和内容未经本站证实,对本文以及其中全部或者部分内容文字的真实性、完整性和原创性本站不作任何保证或承诺,请读者仅作参考,并自行核实相关内容。文章投诉邮箱:anhduc.ph@yahoo.com

    分享
    投诉
    首页