【发布时间】:2021-12-28 09:58:27
【问题描述】:
在 C++20 中,如果应用别名模板,则可以有隐式推导指南。
然后,我构造了一个简单的模板别名,即ints:
template <std::size_t N>
using ints = std::array<int, N>;
但是:
ints{1, 2, 3, 4}
不起作用,GCC说:
- 错误:没有匹配函数调用
array(int, int, int, int) - 注意:无法推导出模板参数
N - 注意:
std::array<int, N>和int的类型不匹配
我不明白为什么编译失败。
还有:
template <typename T>
using array_of_4 = std::array<T, 4>;
与
array_of_4{1, 2, 3, 4}
也不行。
- 是不是因为
std::array的推演指南是用户提供的? - 如果上面的回答不正确,那是什么原因呢?
我发现了一个关于这个问题的类似问题:How to write deduction guidelines for aliases of aggregate templates?。
由此得出结论,按照标准,这段代码应该是格式良好的。因此,GCC 可能有不同的实现来阻止此代码编译。
【问题讨论】:
-
clang 的错误是:
error: alias template 'ints' requires template arguments; argument deduction only allowed for class templates. -
clang 还不支持别名模板的 CTAD (en.cppreference.com/w/cpp/20)
标签: c++ language-lawyer c++20 template-aliases ctad