可变参数模板和标准::绑定
Variadic templates and std::bind
给定以下模板化函数,如何更改它以利用可变参数模板? 也就是说,用可变参数代替 P1 和 P2 替换 std::bind 占位符? 目前,我每个 arity 都有一个这些函数,arity zero 没有 P 参数,直到 arity 9 有 P1 到 P9 参数。 如果可能的话,我希望将其折叠为单个函数。
template<typename R, typename T, typename U, typename P1, typename P2>
void Attach(R (T::*f)(P1, P2), U p)
{
AttachInternal(p, std::bind(f,
p.get(),
std::placeholders::_1,
std::placeholders::_2));
}
您可以(部分)专精std::is_placeholder
自定义模板的专化。这样,您可以通过通常的int_sequence
技术引入占位符生成器。
来自 [func.bind.isplace]/2
实现应提供具有
BaseCharacteristic
integral_constant<int, J>
如果T
是std::placeholders::_J
的类型,否则它的BaseCharacteristic
应为integral_constant<int, 0>
。程序可以将此模板专用于用户定义的类型,T
具有integral_constant<int, N>
BaseCharacteristic
,并N > 0
指示应将T
视为占位符类型。
通常int_sequence
:
#include <cstddef>
template<int...> struct int_sequence {};
template<int N, int... Is> struct make_int_sequence
: make_int_sequence<N-1, N-1, Is...> {};
template<int... Is> struct make_int_sequence<0, Is...>
: int_sequence<Is...> {};
自定义占位符模板和is_placeholder
的专用化:
template<int> // begin with 0 here!
struct placeholder_template
{};
#include <functional>
#include <type_traits>
namespace std
{
template<int N>
struct is_placeholder< placeholder_template<N> >
: integral_constant<int, N+1> // the one is important
{};
}
我不知道在哪里介绍1
;我考虑的地方都不是最佳的。
使用它来编写一些活页夹:
template<class Ret, class... Args, int... Is>
void my_bind(Ret (*p)(Args...), int_sequence<Is...>)
{
auto x = std::bind(p, placeholder_template<Is>{}...);
x( Args(42)... );
}
template<class Ret, class... Args>
void my_bind(Ret (*p)(Args...))
{
my_bind(p, make_int_sequence< sizeof...(Args) >{});
}
活页夹的使用示例:
#include <iostream>
void foo(double, char, int) { std::cout << __PRETTY_FUNCTION__ << "n"; }
void bar(bool, short) { std::cout << __PRETTY_FUNCTION__ << "n"; }
int main()
{
my_bind(foo);
my_bind(bar);
}
我想
提出一个更简单的解决方案,将成员函数绑定到可变数量的占位符:
template<typename R, typename T, typename U, typename... Args>
std::function<R(Args...)> Attach(R (T::*f)(Args...), U p)
{
return [p,f](Args... args)->R { return (p->*f)(args...); };
};
一个简单的用法示例如下所示
class CrazyAdd
{
public:
CrazyAdd(double val)
: m_crazyPart(val)
{}
double add(double a, double b)
{
return a+b+m_crazyPart;
}
private:
double m_crazyPart;
};
void main() {
CrazyAdd myAdd(39);
// Create a function object that is bound to myAdd
auto f = Attach(&CrazyAdd::add, &myAdd);
// Call the function with the parameters
std::cout << f(1,2) << std::endl; // outputs 42
}
就我个人而言,我认为这是Scott Meyer推荐lambdas而不是std::bind的另一个很好的例子。
相关文章:
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 在基于范围的for循环中使用结构化绑定声明
- 使用 LuaBridge 将 LuaJIT 绑定到C++会导致"PANIC: unprotected error"
- 尝试通过OCI例程从Oracle获取blob数据,但出现错误:ORA-01008:并非所有变量都绑定
- 在使用GPU支持编译Tensorflow时,会遇到CUDA_TOOLKIT_PATH未绑定变量
- 视觉studo 2019中的漫画和静态/动态绑定
- 将自由函数绑定为类成员函数
- 将常量指针引用绑定到非常量指针
- 从标准容器重新绑定和复制分配器
- 使用标准::绑定在可变参数模板中完美转发引用
- 可变参数模板和标准::绑定
- 从标准::绑定的转换函数
- 提升::修剪和标准::绑定2nd
- 将r值绑定到l值引用是非标准的Microsoft C++扩展
- 标准::绑定未按预期工作
- 测试标准::绑定到成员函数后的函数有效性
- MSVC和boost::lambda::绑定错误:T0:不允许使用标准参数
- 绑定模板化的标准库函数
- 标准::绑定和完美转发
- 在C++标准中哪里规定常量右值引用不绑定到左值?