在可变参数模板中使用声明
这个问题的灵感来自以下多重继承重载伪解决方案歧义性,这是实现 boost::variant 的 lambda 访问者的好方法,如 这个答案:
我想做类似的事情以下:
template <typename ReturnType, typename... Lambdas>
struct lambda_visitor : public boost::static_visitor<ReturnType>, public Lambdas... {
using Lambdas...::operator(); //<--- doesn't seem to work
lambda_visitor(Lambdas... lambdas) : boost::static_visitor<ReturnType>() , Lambdas(lambdas)... { }
};
我不确定为打包类型列表添加 using 子句的正确语法是什么。 using
子句对于阻止编译器抱怨 operator()
不明确至关重要,而事实并非如此,因为它们具有不同的签名。
This question is inspired in the following solution to multiple inheritance overloading pseudo-ambiguity, which is a nice way to implement lambda visitors for boost::variant as proposed in this answer:
I want to do something like the following:
template <typename ReturnType, typename... Lambdas>
struct lambda_visitor : public boost::static_visitor<ReturnType>, public Lambdas... {
using Lambdas...::operator(); //<--- doesn't seem to work
lambda_visitor(Lambdas... lambdas) : boost::static_visitor<ReturnType>() , Lambdas(lambdas)... { }
};
I'm not sure what would be the right syntax of adding using clauses for packed type lists. The using
clause is crucial to stop the compiler from complaining that the operator()
are ambiguous, which totally are not, because they have all different signatures.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我找到了一个相当不错的解决方案:
基本上我需要解压一个额外的 lambda 案例,并将
using
子句应用于解压的 lambda 和其余部分,但在这种情况下,因为我显然无法制作一个using 声明的可变参数列表(至少我不知道语法,如果可能的话),其余部分通过从“rest”情况继承来包装,如下所示:所以我可以通过放置两个 using 声明来归纳地做到这一点,一个从拆开包装的lambda 类型和父类的另一个,实际上是同一个类,少了一个 lambda。
Ok i found out a pretty decent solution:
basically i need to unpack one extra lambda case and apply the
using
clause to the unpacked lambda and the rest, but in this case, since i apparently i cannot make a variadic list of using declarations (at least i don't know the syntax, if its possible), the rest is wrapped by inheriting from the 'rest' case, like this:So i can do this inductively by placing two using declarations, one from the unpacked lambda type and another from the parent class, which is actually the same class with one less lambda.
这是一个老问题,也是一个很好的答案。恕我直言,我们还可以做一件事来改进它。
在 c++14 及更好的版本中,我们不需要指定返回类型 - 它可以被推断出来。
用例:
This is an old question and a great answer. There's one more thing we can do to improve it IMHO.
In c++14 and better we don't need to specify the return type - it can be deduced.
use case: