具有用户类型的 C++20 模板 <auto> 导致 GCC 9 中的 T/const T 类型不匹配

标签 c++ templates gcc c++20

我正在尝试将非类型模板与自定义类型结合使用。

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/

相关文章:

c++ - 如何使用要在 cmd 中执行的变量创建命令

c++ - 用于添加和比较 Integer 和 Fraction 类数据的运算符重载

C++ - 任何类型的模板类的相同静态方法

c - 为什么在 Vim 中运行 C 代码会跳过 scanf()?

c++ - 生成文件: what's the name of this warning?

c++ - union 体中指针和数组的不同地址

C++:为什么我的 unordered_map<char*, vector<int>> 哈希函数不起作用?

c++ - OpenGL 着色器链接

c++ - 我们什么时候需要 .template 结构

ruby - therubyracer - Gem::Ext::BuildError: 错误:无法构建 gem native 扩展