gpt4 book ai didi

c++ - 是否可以在 C++ 20 中为概念完成删除关键字模板?

转载 作者:行者123 更新时间:2023-11-30 03:13:58 27 4
gpt4 key购买 nike

例如,将 Visual Studio 16.3 与

一起使用

/std:c++最新版

此处声明的标志 here我会写。

#include <concepts>

template <std::integral T>
T plus1(T a) {
return a + 1;
}


int main() {

auto i = plus1(10);

}

但是我不会写

#include <concepts>

std::integral plus1(std::integral a) {
return a + 1;
}


int main() {

auto i = plus1(10);

}

但我读了here这应该是可能的。

最佳答案

Concepts TS 提供了所谓的“简洁语法”,它允许您通过在参数列表中使用概念而不是类型名称来隐式地将函数声明为模板。在尝试将 Concepts TS 纳入标准时,ISO C++ 委员会认为这是有争议的。他们想要一种通过查看函数声明来了解它是否为模板的方法。

经过一番来回,他们came up with an alternate terse syntax :您使用 auto 推导(取自通用 lambda),受概念名称约束:

std::integral auto plus1(std::integral auto a) {
return a + 1;
}

但是,目前大多数概念实现都实现了 Concepts TS 功能,而更新的内容尚未实现。 VS 从来没有 Concepts TS 实现,他们明确表示他们还没有实现这种简洁的模板语法。

关于c++ - 是否可以在 C++ 20 中为概念完成删除关键字模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58289175/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com