谁来提供正确的拉动功能 我想用C++实现“引用流”,引用“”
但我有一个错误:谁来提供正确的拉动功能 我想用C++实现“引用流”,引用“”,c++,C++,但我有一个错误: error: no matching function for call to 'pull' note: candidate function template not viable: requires single argument 'stream', but 3 arguments were provided note: candidate function template not viable: requires 2 arguments, but 3 were prov
error: no matching function for call to 'pull'
note: candidate function template not viable: requires single argument 'stream', but 3 arguments were provided
note: candidate function template not viable: requires 2 arguments, but 3 were provided
我的代码:
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 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);
}
template <typename T>
T pull(T && stream){
return stream;
}
模板
自动记录(R读取){
std::function more=[&](bool done,T val){
如果(!完成){
沉不下去,做错了
int main()
{
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
auto vals = values(vec.begin(), vec.end());
auto mapper = [&](int val){return val * 2;};
auto timesTwo = Map<int>(mapper);
// pull(vals, timesTwo, log);
auto newVals = pull(vals, timesTwo, timesTwo); // work ok
bool ended = false;
do {
newVals(false, [&](bool done, auto val){
ended = done;
if (ended) {
return;
}
cout << val << endl;
});
} while (!ended);
}
intmain()
{
向量向量机;
向量推回(1);
向量推回(2);
自动VAL=值(vec.begin(),vec.end());
自动映射器=[&](int-val){return val*2;};
自动时间2=映射(映射器);
//拉动(VAL、timesTwo、log);
auto newVals=pull(vals,times2,times2);//工作正常
布尔结束=假;
做{
新val(假,[&](布尔完成,自动val){
结束=完成;
若果(完){
返回;
}
无法通过。
正确工作。产出4 8
问题在于log
是一个函数模板,没有任何东西可以推断它是T
和R
,因此变量拉力
过载不匹配
如果log
是一个函数对象,它将起作用,例如
auto log = [](auto read) {
std::function<void (bool, int)> more = [&](bool done, int val){
if(!done){
std::cout << val << std::endl;
read(false, more);
}
};
read(false, more);
};
“谁”将是你:)。你能解释一下为什么你认为你的代码是正确的吗?请用一个或拉(source,through,through)的方法回答你的问题,这样就行了。我还有一个问题()像这样,你能帮我吗
int main()
{
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
auto vals = values(vec.begin(), vec.end());
auto mapper = [&](int val){return val * 2;};
auto timesTwo = Map<int>(mapper);
pull(vals, timesTwo, log); // error, how to write a correct `pull` function, make this line run ok
}
int main()
{
vector<int> vec;
vec.push_back(1);
vec.push_back(2);
auto vals = values(vec.begin(), vec.end());
auto mapper = [&](int val){return val * 2;};
auto timesTwo = Map<int>(mapper);
// pull(vals, timesTwo, log);
auto newVals = pull(vals, timesTwo, timesTwo); // work ok
bool ended = false;
do {
newVals(false, [&](bool done, auto val){
ended = done;
if (ended) {
return;
}
cout << val << endl;
});
} while (!ended);
}
auto log = [](auto read) {
std::function<void (bool, int)> more = [&](bool done, int val){
if(!done){
std::cout << val << std::endl;
read(false, more);
}
};
read(false, more);
};
pull(vals, timesTwo, [](auto read) { log<int>(read); });