浅谈: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
相关推荐
xlxxy_29 分钟前
外部系统调用SAP接口遇到的一些报错
开发语言·数据库·sap·abap
八角.。33 分钟前
方法参数与Debug按键
java·开发语言·jvm
郝亚军1 小时前
FE1.1S-BQFN24BT可以指定IP端口吗?
开发语言·php
阿里嘎多学长1 小时前
2026-08-04 GitHub 热点项目精选
开发语言·程序员·github·代码托管
晴天161 小时前
Rust 快速入门 (从 JavaScript 开发者视角)-Day09
开发语言·javascript·rust
一次旅行1 小时前
RLHF全链路深度解析:Reward Model数学推导+PPO完整实战,对比GRPO轻量化方案
人工智能·算法·机器学习
Wang's Blog1 小时前
AI Agent白手起家29: Few Shot 提示词工程实战
人工智能·算法
步行cgn2 小时前
carList.forEach(System.out::println)
java·开发语言·mybatis
进击的程序猿~2 小时前
Go 内存分配与垃圾回收源码深度学习手册
开发语言·后端·golang
June`2 小时前
warp shuffle指令
c++·人工智能·算法·cuda