本文介绍了使用可变参数模板中的声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题的灵感来自到多重继承重载伪模糊度,这是一个很好的方式来实现lambda的访问者boost :: variant提出在:

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)... { }
};

我不确定对于打包类型列表添加using子句的正确语法。 使用子句对于阻止编译器抱怨 operator()是不明确的,因为他们有所有不同的签名。

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.

推荐答案

我发现一个相当不错的解决方案:

Ok i found out a pretty decent solution:

我需要解包一个额外的lambda case和应用使用子句到解压缩的lambda和其余,但在这种情况下,因为我显然不能做一个变化列表的使用声明(至少我不知道语法,如果可能的话),其余的是从'rest'情况下继承,像这样:

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:

template <typename ReturnType, typename... Lambdas>
struct lambda_visitor;

template <typename ReturnType, typename Lambda1, typename... Lambdas>
struct lambda_visitor< ReturnType, Lambda1 , Lambdas...>
  : public lambda_visitor<ReturnType, Lambdas...>, public Lambda1 {

    using Lambda1::operator();
    using lambda_visitor< ReturnType , Lambdas...>::operator();
    lambda_visitor(Lambda1 l1, Lambdas... lambdas)
      : Lambda1(l1), lambda_visitor< ReturnType , Lambdas...> (lambdas...)
    {}
};


template <typename ReturnType, typename Lambda1>
struct lambda_visitor<ReturnType, Lambda1>
  : public boost::static_visitor<ReturnType>, public Lambda1 {

    using Lambda1::operator();
    lambda_visitor(Lambda1 l1)
      : boost::static_visitor<ReturnType>(), Lambda1(l1)
    {}
};


template <typename ReturnType>
struct lambda_visitor<ReturnType>
  : public boost::static_visitor<ReturnType> {

    lambda_visitor() : boost::static_visitor<ReturnType>() {}
};

所以我可以通过放置两个使用声明,一个来自解包的lambda类型,另一个父类,它实际上是一个少一个lambda的同一个类。

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.

这篇关于使用可变参数模板中的声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 15:28
查看更多