C++ 可变参数模板和模板模板参数:错误:模板参数列表中参数 1 处的类型/值不匹配

c++ variadic templates and template template arguments: error: type/value mismatch at argument 1 in template parameter list

本文关键字:参数 类型 不匹配 错误 变参 C++ 列表      更新时间:2023-10-16

当我编译以下代码时,我收到错误:模板参数列表中参数 1 处的类型/值不匹配...编译器是 gcc 版本 8.2.0。

template<typename>
struct t1 {};
template<typename ...>
struct t2 {};
template<typename, typename ...>
struct t3 {};
template<template<typename> class>
struct tt1 {};
template<template<typename ...> class>
struct tt2{};
template<template<typename, typename ...> class>
struct tt3{};

tt1<t2> _1; // error
tt1<t3> _2; // error
tt2<t1> _3;
tt2<t3> _4;
tt3<t1> _5;
tt3<t2> _6; // error

问题:为什么允许 _3、_4、_5 而 _1、_2_6 是错误的?

这些错误是 C++17 之前的错误。在C++17之前,模板模板参数/参数应完全匹配。

但是由于向标准中添加了P0522R0,因此规则不那么严格,并且此代码可以编译。

截至今天,我认为只有GCC实现了它,您需要指定标准:gcc -std=c++17看到这里。