与可变参数模板和默认参数的模式匹配
我正在尝试将默认的“隐藏”设置添加到模板类别中:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本上与默认函数参数相同:您只能从右侧省略参数。而且我不希望这会改变,因为您想通过添加间接层来实现您要做的事情:
替代:
尽管在这里默认
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:
Alternatively:
Though here the default
false
cannot be really used, for theusing
you still have to specify it.