我如何使用boost ::转化为修改模板构建使用拉姆达的顺序? [英] How do I use boost::transformed to modify a sequence using a lambda in a template construct?

查看:191
本文介绍了我如何使用boost ::转化为修改模板构建使用拉姆达的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类型 MYSTRUCT ,我想以从,其中的每个实例 MYSTRUCT 从传递给它的构造另一范围的元件,以及一些功能的输出产生的。

函数本身( SomeFun )是另一个类的成员函数,但在这个例子中我将使它成为一个免费的功能。但是,请记住,我可以是简单地将其放置在lambda内部或做其他技巧性的东西,以避免一个lambda的需要,因为我需要真正绕过一个这个指针在我的实际情况。

下面是一个简化的,可测试的版本来说明我有这个问题:

 的#include<&iostream的GT;
#包括LT&;矢量>
#包括LT&;升压/范围/适配器/ transformed.hpp>的#include<功能>模板< typename的ROWTYPE>
类MYSTRUCT;模板< typename的T>
INT SomeFun(T T)
{
    返回0;
}模板< typename的序列>
使用转换器=的std ::功能<
    MYSTRUCT< typename的序列:: value_type的>(
        类型名称序列:: VALUE_TYPE&安培;)取代;模板< typename的序列>
提高:: transformed_range<转换器和LT;序列>中序>
    GenerateTransformedRange(常量序列和放大器;输出)
{
    的std ::功能<&MYSTRUCT LT; typename的序列:: value_type的>(INT)>乐趣=
        [](常量类型名称序列:: value_type的输出)
        {
            返回MYSTRUCT< typename的序列:: value_type的>(输出,SomeFun(输出));
        };    返回输出|提高::适配器::转化(乐趣);
}//一些结构
模板< typename的T>
结构MYSTRUCT
{
    MYSTRUCT(T T,诠释一)
    {
    }
};INT主(INT ARGC,为const char * argv的[])
{
    的std ::矢量<&INT GT; vec的{1,2,3};    为(自动元素:GenerateTransformedRange(VEC))
    {
    }    返回0;
}

从这个输出是:


  

的main.cpp:31:54:错误:无法转换
  提振:: range_detail ::操作符|(常量InputRng&放;,常量
  提高:: range_detail :: transform_holder&安培;)与InputRng
  =的std ::向量; UnaryFunction =的std ::功能(INT)>]((*(常量
  提高:: range_detail :: transform_holder(INT)>


  
  

    

)(安培;提高:: range_detail ::转发::运算符()(T)const的[与T =的std ::功能(INT)>;持有人=
    提高:: range_detail :: transform_holder(的std ::函数(INT)>((
的(常量
    的std ::函数(INT)> *)(安培;从娱乐性)))))))
    提振:: range_detail :: transformed_range(INT)>,
    常量的std ::矢量>来
    提振:: range_detail :: transformed_range(INT&安培;)>,
    的std ::矢量>
         返回输出|提高::适配器::转化(乐趣);
                                                          ^生成文件:25:配方目标的.OBJ / main.o中'失败


  

我在做什么错在这里?我不QUOT明白为什么它希望从类型转换。有任何想法吗?我使用升压1.55


解决方案

错误:


  

TEST.CPP:29:20:错误:不对应的操作符|'(操作数类型是常量的std ::向量'和'的std ::功能>(INT)>)
       返回输出|乐趣;


应该是非常清楚。没有运营商| 操作数矢量函数 。你的第二个操作数应该是一个升压适配器,它实际上定义操作。大概是这样的:

 返回输出|提高::适配器::转化(乐趣);

另外,在这种code:

 的std ::功能<&MYSTRUCT LT;序列>(INT)>乐趣=
        [](常量类型名称序列:: value_type的输出)
        {
            返回MYSTRUCT< typename的序列:: value_type的>(输出,SomeFun(输出));
        };

根据该类型声明,乐趣应该返回一个 MYSTRUCT<序列> ,但你的拉姆达返回 MYSTRUCT< typename的序列:: value_type的> 。此外,您可能不应该硬code参数类型为 INT 如果在lambda需要一个序:: VALUE_TYPE 。你或许应该申报乐趣类型转换器和LT;序列> 。此外,修正为λ的参数类型,并匹配转换(注意参考)。

功能

I have a type MyStruct which I would like to produce some "sequence" from, where each instance of MyStruct is generated from passing to its constructor an element of another range as well as the output of some function.

The function itself (SomeFun) is a member function in another class, but for this example I'll make it a free function. However, keep in mind that I can's simply place it inside the lambda or do other tricky things to avoid the need for a lambda because I do need to actually pass a this pointer around in my actual case.

Here is a simplified, testable version to illustrate the problem I'm having:

#include <iostream>                                                             
#include <vector>                                                               
#include <boost/range/adaptor/transformed.hpp>                                  

#include <functional>                                                           

template <typename RowType>                                                     
class MyStruct;                                                                 

template <typename T>                                                           
int SomeFun(T t)                                                                
{                                                                               
    return 0;                                                                   
}                                                                               

template <typename Sequence>                                                    
using Converter = std::function<                                                
    MyStruct<typename Sequence::value_type>(                                    
        typename Sequence::value_type&)>;                                       

template <typename Sequence>                                                    
boost::transformed_range<Converter<Sequence>, Sequence>                         
    GenerateTransformedRange(const Sequence& outputs)                           
{                                                                               
    std::function<MyStruct<typename Sequence::value_type>(int)> fun =           
        [](const typename Sequence::value_type output)                          
        {                                                                       
            return MyStruct<typename Sequence::value_type>(output, SomeFun(output));
        };                                                                      

    return outputs | boost::adaptors::transformed(fun);                         
}                                                                               

// some structure                                                               
template <typename T>                                                           
struct MyStruct                                                                 
{                                                                               
    MyStruct(T t, int a)                                                        
    {                                                                           
    }                                                                           
};                                                                              

int main(int argc, const char* argv[])                                          
{                                                                               
    std::vector<int> vec {1, 2, 3};                                             

    for (auto element : GenerateTransformedRange(vec))                          
    {                                                                           
    }                                                                           

    return 0;                                                                   
}

The output from this is:

main.cpp:31:54: error: could not convert ‘boost::range_detail::operator|(const InputRng&, const boost::range_detail::transform_holder&) [with InputRng = std::vector; UnaryFunction = std::function(int)>]((*(const boost::range_detail::transform_holder(int)>

)(& boost::range_detail::forwarder::operator()(T) const [with T = std::function(int)>; Holder = boost::range_detail::transform_holder](std::function(int)>(((const std::function(int)>*)(& fun)))))))’ from ‘boost::range_detail::transformed_range(int)>, const std::vector >’ to ‘boost::range_detail::transformed_range(int&)>, std::vector >’ return outputs | boost::adaptors::transformed(fun); ^ makefile:25: recipe for target '.obj/main.o' failed

What am I doing wrong here? I don't quot understand why it wants to convert from that type. Any ideas? I'm using Boost 1.55.

解决方案

The error:

test.cpp:29:20: error: no match for ‘operator|’ (operand types are ‘const std::vector’ and ‘std::function >(int)>’) return outputs | fun;

should be perfectly clear. There is no operator| for operands vector and a function. Your second operand should be a boost adaptor which actually defines the operator. Probably like this:

return outputs | boost::adaptors::transformed(fun);

Also in this code:

std::function<MyStruct<Sequence>(int)> fun =
        [](const typename Sequence::value_type output)
        {
            return MyStruct<typename Sequence::value_type>(output, SomeFun(output));
        };

According to the type declaration, fun should return a MyStruct<Sequence> but your lambda returns a MyStruct<typename Sequence::value_type>. Also, you probably shouldn't hardcode the parameter type as int if the lambda expects a Sequence::value_type. You should probably declare fun as type Converter<Sequence>. Also, fix the parameter type of the lambda and the function to match with Converter (pay attention to the reference).

这篇关于我如何使用boost ::转化为修改模板构建使用拉姆达的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆