gpt4 book ai didi

c++ - 如何在 rxcpp 自定义运算符中正确推断泛型

转载 作者:行者123 更新时间:2023-11-28 05:19:11 25 4
gpt4 key购买 nike

我创建了一个名为 validateImplementation 的自定义 rxcpp 运算符,它应该简单地采用通用可观察流,在 SimpleInterface 上进行一些验证,然后继续或结束基于流在特定条件下(在我的例子中,条件是 whatsMyId)

https://github.com/cipriancaba/rxcpp-examples/blob/master/src/SimpleOperators.cpp

template <class T> function<observable<T>(observable<T>)> SimpleOperators::validateImplementation(SimpleInterface component) {
return [&](observable<T> $str) {
return $str |
filter([&](const T item) {
if (component.whatsMyId() == "1") {
return true;
} else {
return false;
}
}
);
};
}

但是,当尝试在 main.cpp 中使用 validateImplementation 方法时,出现以下错误:

没有匹配的成员函数来调用“validateImplementation”

注意:候选模板被忽略:无法推断模板参数“T”

你能帮我理解我做错了什么吗?

最佳答案

在 C++ 中,必须先完全解析类型,然后才能使用该函数。此外,模板参数只能从参数中推断出来,而不能从返回类型中推断出来。最后,带有模板参数的函数的定义在被调用(在 header 中)或为每个支持的类型(在 cpp 中)显式实例化时必须是可见的。

在这种情况下,我会避免显式实例化。这意味着有两种选择。

移除模板参数

function<observable<string>(observable<string>)> validateImplementation(SimpleInterface component);

将定义从 cpp 移动到标题更改 main.cpp 以明确类型,因为它无法推断。

o->validateImplementation<string>(s1) |

关于c++ - 如何在 rxcpp 自定义运算符中正确推断泛型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41877100/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com