如何获取指向已推断的模板函数的指针?
How could I get a pointer to an already-deduced template function?
我有一个模板函数,我想在自动推断其模板参数的情况下使用它。
它运行良好,但现在我需要它的ptr
用于std::bind
.
它是这样的:
class A{};
class B{};
class C{};
template<template<typename...> class TContainer, typename TR, typename... TEles>
void func(TContainer<TEles...> container, function<TR(HeadOf<TEles...>::type)> f)
{
TContainer<TR> rst;
for(auto it : container)
{
fill(rst, f(it));
}
return rst;
}
vector<A> vec_A; // type : vector<A, allocator<A>>
MyContainer<B> cont_B; // type : MyContainer<B, allovator<B>, Maybe_Sth_Else>
// auto ptrFoo = func_ptr_of( func(vec_A, transA2B) );
// auto ptrBar = func_ptr_of( func(cont_B, transB2C) );
// auto contB2contC = std::bind(ptrBar, placeholder::_1, transB2C);
// contB2contC(cont_B);
我认为这是不可能的。
但是如果你有一个 C++14 编译器,你可以用可变参数 lambda 做类似的事情。
通过示例
A a{};
auto fakeBondFunc = [&](auto ... as) { func(a, as...); };
fakeBondFunc(B{});
如果你想修复第一个A
参数并强制要求第二个参数是B
参数,你也可以在 C++11 中执行此操作(不需要可变参数 lambda(
A a{};
auto fakeBondFunc = [&](B const & b) { func(a, b); };
fakeBondFunc(B{});
using ptr_t = void(*)(A, B);
ptr_t ptrFoo = func<A, B>;
std::function<void(B)> boundFunc = std::bind(ptrFoo, A{}, std::placeholders::_1);
boundFunc(B{});
相关文章:
- 如何正确编写指针函数声明?
- C++常规指针函数或模板
- 如何重新定义 C++ 指针函数?
- C++ 指向其他类函数的指针函数
- 指针到指针函数参数
- 将指向成员的指针函数传递到模板中
- C++ 在 none 常量指针函数中返回一个常量指针
- 如何使用指针函数编写/读取数组
- 是C 中的函数指针函数对象
- 如何从另一个类调用指向成员的指针函数
- 如何声明采用指向成员的指针函数的函数
- 如何构造一个以可变参数指针函数作为成员的类?
- c 通过值或指针函数语法
- 将typedef方法作为指针函数传递
- 从 Main 中的双指针函数打印出指针数组
- strcpy 对指针函数的引用
- 这是否仍然声明一种指针函数的别名?
- 将指向成员的指针函数与 std::shared_ptr 结合使用
- 'Incomplete type' 为标准::函数声明指向成员的指针函数模板参数时出错
- 从注入进程的 DLL 调用函数并更改指针函数的地址