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

cpp 14 ~ cpp 17

对于每次写std::is_integral<T>::value和std::enable_if<B>::type都比较麻烦。

因此 cpp 14 建议可以通过另一个简单的符号表示该内容。也就是type trait variable templates的概念。

直到 cpp 17 才在正式标准中进行了全面的完善。

复制代码
// cpp14
template< bool B, class T = void >
using enable_if_t = typename enable_if<B,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 20

c++ 是一门不断发展的现代语言,在 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_v<Iter>)
    vector(Iter begin, Iter end) {
        std::cout << "vector(Iter begin, Iter end)" << std::endl;
    }
};
    
}  // namespace my
相关推荐
迪康Defender2 分钟前
终端安全事后追溯能力解析:本地审计模块设计思路与落地实践
运维·开发语言·人工智能·安全·php·安全威胁分析·运维开发
(Charon)4 分钟前
【C++面试】迭代器失效是什么?vector、list、map中如何避免迭代器失效
c++·算法·面试
yujunl5 分钟前
开发一个可以用APIFOX来调试的接口
开发语言
苏辰澈22 分钟前
C++自己的族谱规则:继承详解下
c语言·开发语言·c++·visualstudio
hunter19901026 分钟前
英语作文题目
开发语言
谢亮_vipxieliang41 分钟前
Go基础数据类型全解
开发语言·后端·golang
泡海椒43 分钟前
JQuick-Excel 导入反向映射实战:让中文表头稳定落到业务字段
开发语言·python·excel
空堂与归1 小时前
AI 与 SI(超级智能):先分清窄智能与超级智能的边界
开发语言·人工智能·gpt
熊猫钓鱼>_>1 小时前
Kotlin Multiplatform for OpenHarmony 实战:为 kotlin-inject 实现依赖注入适配
开发语言·华为·kotlin·ai编程·inject·鸿蒙·openharmony
Wang's Blog1 小时前
Java框架 SpringCloud 快速入门: 网关的 CORS 跨域配置
java·开发语言·spring cloud