我想知道是否可以从函数模板创建函数对象。以下Functor结构从函数创建函数对象:
#include <iostream>
template <typename F, F* f>
struct Functor {
template <typename... Ar>
auto operator()(Ar&&... rg) -> decltype(f(std::forward<Ar>(rg)...)) {
return f(std::forward<Ar>(rg)...);
}
};
int plus_one(int a) {
return a + 1;
}
int main () {
std::cout << Functor<decltype(plus_one), &plus_one>()(1) << "\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这会创建一个类似的结构
struct PlusOne {
int operator()(int a) {
return a + 1;
}
}
Run Code Online (Sandbox Code Playgroud)
现在,我想使用函数模板而不是函数:
template <typename T>
T plus_one(T a) {
return a + 1;
}
Run Code Online (Sandbox Code Playgroud)
我希望能变成类似的东西
struct PlusOne {
template …Run Code Online (Sandbox Code Playgroud) 有没有办法执行以下操作
ex1 <- quote(iris)
ex2 <- quote(dplyr::filter(Species == "setosa" & Sepal.Width > 4))
substitute(x %>% y, list(x = ex1, y = ex2))
#> iris %>% filter(Species == "setosa" & Sepal.Width > 4)
Run Code Online (Sandbox Code Playgroud)
使用基管而不是 Magrittr 管?
substitute(x |> y, list(x = ex1, y = ex2))
#> Error: The pipe operator requires a function call as RHS
Run Code Online (Sandbox Code Playgroud)