【发布时间】:2020-05-21 17:31:17
【问题描述】:
对于以后遇到这个问题的人来说,最初的问题是:“如何从这个article 获得最简单的示例(点)以使用 GCC 或 CLANG?”
- Edit 1 显示了使用 CLANG 失败但使用 GCC 编译的最小可能代码(均使用 -std=c++2a)。
- Edit 2 显示了添加的更多代码,这也破坏了 GCC。
文章的作者 (@BarryRevzin) 非常友好地评论了为什么这还行不通的原因,谢谢 Barry!
编辑 1:
下面的简化代码适用于 gcc 9.3.0,但不适用于 clang 10.0.0:
struct Point {
int x = 0;
int y = 0;
};
template <Point> // ok in C++20
void takes_tmpl_point();
int main()
{
// EMPTY
}
编辑 2:
根据作者的说法,由于编译器有点落后于标准,原始代码目前还不能在 GCC 或 CLANG 上运行。原代码如下:
struct Point {
int x = 0;
int y = 0;
};
template <Point> // ok in C++20
void takes_tmpl_point();
int main()
{
takes_tmpl_point<{.x=1, .y=2}>(); // x=1, y=2
}
这将导致在 GCC 9.3 上出现以下编译错误:
test.cpp: In function ‘int main()’:
test.cpp:11:35: error: no matching function for call to ‘takes_tmpl_point<{1, 2}>()’
11 | takes_tmpl_point<{.x=1, .y=2}>(); // x=1, y=2
| ^
test.cpp:7:6: note: candidate: ‘template<Point <anonymous> > void takes_tmpl_point()’
7 | void takes_tmpl_point();
| ^~~~~~~~~~~~~~~~
test.cpp:7:6: note: template argument deduction/substitution failed:
test.cpp:11:35: error: could not convert ‘{1, 2}’ from ‘<brace-enclosed initializer list>’ to ‘Point’
11 | takes_tmpl_point<{.x=1, .y=2}>(); // x=1, y=2
| ^
并且在 clang 10.0.0 上出现以下错误:
test.cpp:6:16: error: a non-type template parameter cannot have type 'Point'
template <Point> // ok in C++20
^
test.cpp:11:21: error: expected expression
takes_tmpl_point<{.x=1, .y=2}>(); // x=1, y=2
^
2 errors generated.
使用的编译器:
- clang:clang 版本 10.0.0-4ubuntu1
- gcc: gcc (Ubuntu 9.3.0-10ubuntu2) 9.3.0
【问题讨论】:
-
OK:您的编译器是否声称符合所有 C++20?
-
GCC 9.3 对你最近的例子没问题。