与可变参数模板和默认参数的模式匹配

Pattern matching with variadic templates and default argument

我正在尝试将默认的“隐藏”设置添加到模板化 class:

template<bool DebugMode=false, typename... Args>
struct A
{
        A() {};
};

int main()
{
    A<double, double> a;
}

使用 g++ 8.3.1 和 C++17 编译时失败:

error: type/value mismatch at argument 1 in template parameter list for ‘template<bool DebugMode, class ... Args> struct A’
note:  expected a constant of type ‘bool’, got ‘double’

但我不明白为什么 g++ 不能在模板参数中进行任何模式匹配。是否会修复更新的 C++ 版本?

与默认函数参数基本相同:只能省略右边的参数。而且我不希望这会改变,也是因为你想做的事情可以通过添加一个间接层来实现:

template<bool DebugMode=false>
struct Wrap {
    template <typename ...T> struct A {};
};

template <typename...T> using A = Wrap<>::A<T...>;

int main() {
    A<double, double> a;
}

或者:

template <bool DebugMode=false,typename ...T>
struct A_impl {};

template <typename...T>
using A = A_impl<false,T...>;

这里虽然不能真正使用默认的false,但是对于using还是要指定的