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

发布于 2025-01-17 23:57:12 字数 489 浏览 2 评论 0原文

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

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 ++版本吗?

I'm trying to add a default "hidden" setting into a templated class:

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

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

which fails when compile with g++ 8.3.1 and 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’

Yet I don't understand why g++ can't do any pattern matching in template arguments. Will it be fixed a newer C++ version ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牛↙奶布丁 2025-01-24 23:57:12

基本上与默认函数参数相同:您只能从右侧省略参数。而且我不希望这会改变,因为您想通过添加间接层来实现您要做的事情:

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无法真正使用,但对于使用您仍然必须指定它。

It's basically the same as with default function arguments: You can only omit parameters from the right. And I don't expect this to change, also because what you want to do can be achieved by adding a layer of indirection:

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;
}

Alternatively:

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

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

Though here the default false cannot be really used, for the using you still have to specify it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文