通过C+实现pull流+;带分段故障 我想用C++实现“引用流”,引用“”

通过C+实现pull流+;带分段故障 我想用C++实现“引用流”,引用“”,c++,C++,但我得到了错误:分段错误,当我链接read->(through->through)->sink时 我的代码 template <typename T> auto values(T && begin, T && end){ return [&](bool abort, auto cb){ if(begin != end){ cb(false, *begin++); }

但我得到了错误:分段错误,当我链接read->(through->through)->sink时

我的代码

template <typename T>
auto values(T && begin, T && end){
    return [&](bool abort, auto cb){

        if(begin != end){
            cb(false, *begin++);
        }
        else{
            cb(true, *begin);
        }
    };
}
模板
自动值(T&&begin、T&&end){
返回[&](布尔中止,自动cb){
如果(开始!=结束){
cb(false,*begin++);
}
否则{
cb(真,*开始);
}
};
}
源/读,包含值

template <typename T, typename M>
auto Map(M mapper) {

    return [&](auto & read){
        return [&](bool abort, auto cb){
            read(abort, [&](bool end, T val){
                if(end)
                    cb(true, val);
                else
                    cb(false, mapper(val));
            });
        };
    };
}
模板
自动映射(M映射器){
返回[&](自动读取(&R)){
返回[&](布尔中止,自动cb){
读取(中止,[&](布尔结束,T值){
若(完)
cb(真,val);
其他的
cb(假,映射器(val));
});
};
};
}
使用映射器生成一个直通的映射


template <typename T, typename R>
auto log(R && read){

    std::function<void (bool, T)> more = [&](bool done, T val){
        if(!done){
            cout << val << endl;
            read(false, more);
        }
    };

    read(false, more);
}

模板
自动日志(R&&read){
std::function more=[&](bool done,T val){
如果(!完成){
穿过去
模板
自动拉入(R&&read、S&&sink){
返回水槽(读取);
}
拉,链接读取->通过或通过->通过


// return read
// read -> through -> ...
template <typename R, typename T, typename... Ts>
auto pull(R && read, T && through, Ts &&... args){
    return pull(through(read), std::forward<Ts>(args)...);
}

//返回读取
//读取->通过->。。。
模板
自动拉取(R&&read、T&&through、Ts&&args){
返回拉力(通过(读取),标准::向前(args);
}
拉,链接源->通过->接收


// return through
// through -> through
// through -> sink
template <typename T, typename R>
auto pull2(T && through1, R && through2){
    return [&](auto & read) {
        return pull(read, through1, through2);
    };
}

int main()
{
    vector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    auto begin = vec.begin();
    auto end = vec.end();

    auto logInt = [&](auto read) { log<int>(read); };
    auto vals = values(begin, end);

    auto mapper = [&](int val){return val * 2;};
    auto timesTwo = Map<int>(mapper);

    auto valsTimes = pull(vals, timesTwo);

    auto timesFour = pull2(timesTwo, timesTwo);

    auto doubleSink = pull2(timesTwo, logInt);

    pull(vals, timesTwo, timesTwo, logInt); // work right
   // pull(vals, timesFour, logInt); // error, Segmentation fault, 
                                     // may be pull/pull2/Map function has bug, but I don't known where, 
                                     //how to correct it to make this line run ok

    return 0;
}

//回程
//通过->通过
//通过->水槽
模板
自动拉2(T和穿过1,R和穿过2){
返回[&](自动读取(&R)){
回拉(读取、通过1、通过2);
};
}
拉,链接通过->通过或通过->下沉


// return through
// through -> through
// through -> sink
template <typename T, typename R>
auto pull2(T && through1, R && through2){
    return [&](auto & read) {
        return pull(read, through1, through2);
    };
}

int main()
{
    vector<int> vec;
    vec.push_back(1);
    vec.push_back(2);
    auto begin = vec.begin();
    auto end = vec.end();

    auto logInt = [&](auto read) { log<int>(read); };
    auto vals = values(begin, end);

    auto mapper = [&](int val){return val * 2;};
    auto timesTwo = Map<int>(mapper);

    auto valsTimes = pull(vals, timesTwo);

    auto timesFour = pull2(timesTwo, timesTwo);

    auto doubleSink = pull2(timesTwo, logInt);

    pull(vals, timesTwo, timesTwo, logInt); // work right
   // pull(vals, timesFour, logInt); // error, Segmentation fault, 
                                     // may be pull/pull2/Map function has bug, but I don't known where, 
                                     //how to correct it to make this line run ok

    return 0;
}

int main()
{
向量向量机;
向量推回(1);
向量推回(2);
自动开始=向量开始();
自动结束=向量结束();
自动登录=[&](自动读取){log(读取);};
自动VAL=值(开始、结束);
自动映射器=[&](int-val){return val*2;};
自动时间2=映射(映射器);
自动VALTIMES=拉动(VAL,timesTwo);
自动timesFour=pull2(timesTwo,timesTwo);
auto doubleSink=pull2(times2,logInt);
pull(VAL、timesTwo、timesTwo、logInt);//正常工作
//pull(VAL,timesFour,logInt);//错误,分段错误,
//可能pull/pull2/Map函数有bug,但我不知道在哪里,
//如何纠正它使这条线正常运行
返回0;
}
谁来做这行
pull(VAL、timesFour、logInt);
run ok


请阅读,尤其是和。最后,不要忘记如何创建一个问题本身,使其成为一个独立的问题。你能在一个代码段中发布所有代码段,例如带有注释,以便易于复制和测试吗?你能添加相关的
#include
吗?你能创建一个可编译的吗您的问题是什么?代码示例: