- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
如何使用C++模板调用匹配的函数?例如,如果我有函数 a 和 b:
void a_impl(string, int){}
void b_impl(int, string){}
template<typename X, typename Y>
void a(X x, Y y){
a_impl(x, y);
}
template<typename X, typename Y>
void b(X x, Y y){
b_impl(x, y);
}
template<typename X, typename Y>
void a_or_b(X x, Y y);
我如何实现 a_or_b
使其在匹配时调用 a(x, y)
,否则调用 b(x, y)
?
我想要做的是一个可以处理这些情况的 for_each
函数:
vector<pair<string, int>> v1 = {{"one", 1}, {"two", 2}};
for_each(v1, [](string x, int y){
cout << x << " " << y << endl;
});
vector<int> v2 = {1, 2, 3};
for_each(v2, [](int x){
cout << x << endl;
});
到目前为止,我已经独立地为元组和单个变量工作,但我希望自动选择适当的版本。到目前为止,这是我的实现; unpack
是来自此页面的apply_from_tuple
http://www.cppsamples.com/common-tasks/apply-tuple-to-function.html .
template<typename Range, typename Func>
void for_each_unpack(Range && range, Func && func){
for (auto && element : range){
using Element = decltype(element);
unpack(std::forward<Func>(func), std::forward<Element>(element));
}
}
template<typename Range, typename Func>
void for_each_nounpack(Range && range, Func && func){
for (auto && element : range){
using Element = decltype(element);
std::forward<Func>(func)(std::forward<Element>(element));
}
}
编辑:多亏了@jotik,它才开始工作。我把代码放在github上https://github.com/csiz/for_each .
最佳答案
使用 decltype
的尾随返回类型和 SFINAE :
#include <iostream>
#include <string>
#include <utility>
void a(std::string, int) { std::cout << "a" << std::endl; }
void b(int, std::string) { std::cout << "b" << std::endl; }
template <typename ... Args>
auto a_or_b(Args && ... args)
-> decltype(a(std::forward<Args>(args)...))
{ return a(std::forward<Args>(args)...); }
template <typename ... Args>
auto a_or_b(Args && ... args)
-> decltype(b(std::forward<Args>(args)...))
{ return b(std::forward<Args>(args)...); }
int main() {
std::string s;
int i;
a_or_b(s, i); // calls a
a_or_b(i, s); // calls b
}
我在上面的例子中使用了完美转发,因为它避免了每个参数的拷贝,但是具有显式类型的不太通用的朴素解决方案也有效:
template <typename X, typename Y>
auto a_or_b(X x, Y y) -> decltype(a(x, y))
{ return a(x, y); }
template <typename X, typename Y>
auto a_or_b(X x, Y y) -> decltype(b(x, y))
{ return b(x, y); }
SFINAE 在这种情况下的工作方式如下。请注意,a_or_b
有 2 个模板定义。 .当你写一个函数调用给 a_or_b
编译器试图找出哪个 a_or_b
你本来想打电话的由于 SFINAE,它忽略任何模板 a_or_b
它无法推断出其类型。例如。电话 a_or_b(s, i);
(尾随)返回类型 decltype(b(std::forward<Args>(args)...))
第二个a_or_b
定义不起作用,因此第二个 a_or_b
编译器不考虑定义。
在这种情况下,返回类型必须是尾随返回类型,因为它取决于函数参数。例如,以下不会编译:
template <typename ... Args>
decltype(b(std::forward<Args>(args)...)) a_or_b(Args && ... args)
{ return b(std::forward<Args>(args)...); }
关于C++ 模板 : call whichever function matches between 2 choices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35959463/
我一直在努力缩短这行代码: const priceSet = price.value > 0 ? price.value : 0; 我只是想知道是否有更聪明的方法来编写它。这不重要,这不是优先事项,我
这个问题在这里已经有了答案: Force an image to fit and keep aspect ratio (2 个答案) 关闭 5 年前。
如何使用C++模板调用匹配的函数?例如,如果我有函数 a 和 b: void a_impl(string, int){} void b_impl(int, string){} template voi
我是 Python 新手。我已经浏览了其他答案..我可以肯定地说,这可能不是重复的。 基本上;举例来说,我想查找其中一个子字符串(存储在列表中)的出现情况;如果找到了?我希望它停止搜索列表的其他子字符
我是一名优秀的程序员,十分优秀!