【发布时间】:2020-06-07 00:49:53
【问题描述】:
C++20 允许使用auto 作为函数参数类型。
是否还允许将auto 用作函数参数类型的模板参数占位符(不相似,但在某种程度上符合C++17 template<auto> 的精神)?
所以下面的代码,pre C++20:
template<typename First, typename Second>
void printPair(const std::pair<First, Second>& p) {
std::cout << p.first << ", " << p.second;
}
可以写成:
void printPair(const std::pair<auto, auto>& p) {
std::cout << p.first << ", " << p.second;
}
它does compile and works nicely 带有实验性的 GCC 概念实现。
它是 C++20 的合法语法吗?
相关:Wildcard for C++ concepts saying "accepting anything for this template argument"
【问题讨论】:
-
据我所知,不受约束的
auto直接 转换为模板化的typename XYZ,这强烈暗示它是合法的语法。 整洁. -
请注意,Clang disagrees 和 Clang 和 GCC 对于
[](const std::pair<auto, auto>& p){}中是否允许使用auto存在相同的分歧(无论是-std=c++2a还是-std=c++17)。 -
谢谢@DavisHerring - 我已经修正了措辞