我正在尝试将非类型模板与自定义类型结合使用。
struct T {};
template <auto value> struct U {};
template <auto value>
void f (U <value>) {}
int main()
{
constexpr T t;
f (U<1>{}); // OK
f<t> (U<t>{}); // OK
f (U<t>{}); // Error
}
模板参数推导失败,gcc trunk with -std=c++2a gets
yop.cpp:10:5: note: template argument deduction/substitution failed:
yop.cpp:19:21: note: mismatched types ‘T’ and ‘const T’
19 | f (U<t>{}); // Error
| ^
我是不是遗漏了什么或者这是一个错误?
最佳答案
好的,正在浏览最新的草稿...
[temp.arg.nontype] 说
If the type T of a template-parameter (12.1) contains a placeholder type (9.1.7.5) or a placeholder for a deduced class type (9.1.7.6), the type of the parameter is the type deduced for the variable x in the invented declaration
T x = template-argument ;
作为插入
auto x = t;
static_assert (std::is_same_v <decltype (x), T>);
在上面的代码片段中,我提交了一个错误。
关于具有用户类型的 C++20 模板 <auto> 导致 GCC 9 中的 T/const T 类型不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53534459/