浅谈: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
相关推荐
1001101_QIA11 分钟前
.exe does not seem to be a Qt executable.
开发语言·qt
小蒋观天下31 分钟前
雷击与浪涌防护:前端SPD选型、接地网误区,接地不当反而引入干扰
深度学习·算法·机器学习
echome88835 分钟前
Python 异常处理:try、else、finally 的正确搭配
开发语言·前端·python·异常处理·编程技巧
云和数据.ChenGuang36 分钟前
Python 单继承与多继承深度剖析:从 MRO 到实战取舍
开发语言·人工智能·python·langchain
小咕聊编程1 小时前
基于Python的豆瓣电影数据可视化系统构建分析
开发语言·python·信息可视化
WBluuue2 小时前
数据结构与算法:分块(一):块状数组、块状链表
数据结构·c++·算法·链表
脏脏a3 小时前
延迟与画质:远程控制的生命线
开发语言·php
澳鹏Appen3 小时前
澳鹏重磅发布ExamOCRBench:面向端到端试卷判分的多任务基准
算法·benchmark·基准评测
像风一样的男人@3 小时前
python --基于scp的文件传输助手
开发语言·python
一条破秋裤3 小时前
SDIO Wi‑Fi 设备的驱动向insmod + 反向rmmod
开发语言·php