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

cpp 14 ~ cpp 17

对于每次写std::is_integral<T>::valuestd::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
相关推荐
M78佐菲几秒前
Linux学习笔记:进程
linux·笔记·学习·算法
liangshanbo121516 分钟前
虚拟列表深度面试题整理
java·开发语言·前端
ZISHU_98720 分钟前
用 TLabel 给 SynTouch BioTac 数据做语义标注:从原始信号到结构化标注
开发语言·人工智能·python·数据·机器人触觉
略略略咯咯27 分钟前
C#Unity
开发语言·unity·c#
gugucoding28 分钟前
59. 【Java】Spring Boot 入门:第一个 Web 应用
java·开发语言·spring boot
ShineWinsu36 分钟前
对于C++:C++20中线程、初始化、Lambda与内存视图等特性的解析
c++·c++20
lilian2331 小时前
Harmony os 技术实战|拼豆制图43:压缩字符矩阵上线前如何拦住错位图纸
开发语言·前端·华为·矩阵·harmonyos
进击ing小白1 小时前
Qt 无边框窗口进阶指南
开发语言·qt
点我头像干啥1 小时前
004:多个PDF文件合并
开发语言·pdf·php
En^_^Joy1 小时前
Cadence Skill脚本语言快速入门指南
开发语言