浅谈:C++中cpp 14 ~ cpp 17

浅谈:C++中cpp 14 ~ cpp 17 cpp 14 ~ cpp 17对于每次写std::is_integralT::value和std::enable_ifB::type都比较麻烦。因此 cpp 14 建议可以通过另一个简单的符号表示该内容。也就是type trait variable templates的概念。直到 cpp 17 才在正式标准中进行了全面的完善。// cpp14 template bool B, class T void using enable_if_t typename enable_ifB,T::type; // cpp17 template class T 因此我们可以得到以下的简洁版本。#include iostream #include type_traits #include vector namespace my { template typename Type class vector { public: vector(size_t len, Type val) { std::cout vector(size_t len, Type val) std::endl; } /**cpp 20c 是一门不断发展的现代语言在 cpp 20 中提出了概念和约束到标准中。requires是一个关键字。可以直接在模板函数中进行使用。requires是在template和函数体之间编写提升可代码可阅读性。注意一点requires 子句需要是一个初等表达式 或者 带括号的表达式。#include concepts #include iostream #include type_traits #include vector namespace my { template typename Type class vector { public: vector(size_t len, Type val) { std::cout vector(size_t len, Type val) std::endl; } // 使用 requires 关键字 // 直接写出约束条件 template typename Iter requires (!std::is_integral_vIter) vector(Iter begin, Iter end) { std::cout vector(Iter begin, Iter end) std::endl; } }; } // namespace my