Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如果没有模板的特定案例,如何存储std::bind的向量?_C++_Vector_Std Function_Stdbind - Fatal编程技术网

C++ 如果没有模板的特定案例,如何存储std::bind的向量?

C++ 如果没有模板的特定案例,如何存储std::bind的向量?,c++,vector,std-function,stdbind,C++,Vector,Std Function,Stdbind,在浏览了std::bind之后,我想知道是否可以保存由std::bind创建的函数的vector,这样我就可以避免使用std::function及其重量级包装 #include <iostream> #include <functional> #include <typeinfo> #include <vector> int add(int a, int b) {return a + b;} int main() { //I beli

在浏览了
std::bind
之后,我想知道是否可以保存由
std::bind
创建的函数的
vector
,这样我就可以避免使用
std::function
及其重量级包装

#include <iostream>
#include <functional>
#include <typeinfo>
#include <vector>

int add(int a, int b) {return a + b;}

int main() {

    //I believe this here is just a special type of bound function.
    auto add2 = std::bind(add, std::placeholders::_1, 2);
    auto add3 = std::bind(add, std::placeholders::_1, 3);

    //Yup.
    std::cout << typeid(add2).name() << std::endl;
    //Here's the type of the second function
    std::cout << typeid(add3).name() << std::endl;

    //Is there a nicer way to do this?
    std::vector<decltype(std::bind(add, std::placeholders::_1, 1))> vec;

    return 0;   
}
#包括
#包括
#包括
#包括
intadd(inta,intb){返回a+b;}
int main(){
//我相信这只是一种特殊的有界函数。
自动添加2=std::bind(添加,std::占位符::_1,2);
自动添加3=std::bind(添加,std::占位符::_1,3);
//是的。
这个怎么样

#include <iostream>
#include <functional>
#include <vector>

int add(int a, int b) { return a + b; }

using bound_add_t = decltype(std::bind(add, std::placeholders::_1, int()));

int main() {
  std::vector<bound_add_t> vec;
  vec.emplace_back(add,std::placeholders::_1, 1);
  vec.emplace_back(add,std::placeholders::_1, 2);
  vec.emplace_back(add,std::placeholders::_1, 3);

  for (auto &b : vec)
    std::cout << b(5) << std::endl;

  return 0;   
}
#包括
#包括
#包括
intadd(inta,intb){返回a+b;}
使用bound_add_t=decltype(std::bind(add,std::placeholders::_1,int());
int main(){
std::vec;
向量放置回(添加,标准::占位符::1,1);
向量放置回(添加,标准::占位符::1,2);
向量放置回(添加,标准::占位符::1,3);
对于(自动和b:vec)

std::cout因此,这似乎是不可能的--
std::bind
似乎没有一个通常可命名/显式的类型--该类型通常是根据绑定函数的签名生成的,并且似乎不可指定。使用
std::function
似乎是包装
std::bind
函数的唯一方法并将它们存储在向量中

--使用
std::function
形式的包装器似乎是解决方法,尽管结果函数对象的大小增加了


可能的替代方案可能是存储
Functor
对象,这些对象被设计为模拟闭包,并与带有类型检查层的泛型对象耦合(尽管这看起来相当繁重).无论如何,使用
std::function
似乎是最干净的解决方案。

为什么不
std::vector vec;
?或者您是否假设希望
放置每个向量元素?我希望能够在不提供任何绑定函数的情况下进行初始化。因此,现在没有内联
std::bind
或绑定函数。嗯,
bind
返回一个未指定的类型,因此您需要一个
bind
表达式来推断类型。无论如何,
vector
的实用性是相当有限的,因为更改该
bind
表达式中的绑定参数以外的任何内容都会更改结果类型,所以您可以这样做没有太多的
bind
表达式可供您添加到
vector
@Praetorian:甚至可以保证吗?我希望实现可以在类型中免费编码整数文本(高于
2
3
)(例如,作为模板参数,尽管我们在讨论实现时,它甚至不需要通过用户暴露的标准C++特性)……“奇怪的是,但是返回一个未指定的类型”留下了很大的余地。“因此我可以避免使用std::function及其重量级包装”,相对于使用
bind
时的权重,因此您表明,至少在您的实现中——使用某些编译器选项等——有避免
function
的实际动机,而不仅仅是FUD。(FWIW,当GCC在coliru上时,我看到
函数的大小为32字节,而
绑定的大小为16字节,但除此之外可能还有间接内存使用)不,对不起,我想要一个真正的解决方案,而不仅仅是一个使用
别名的简单的
。我真的认为,如果你想保持可移植性,你所要求的是不可能的。
std::bind
构造函数返回一个
/*未指定的*/
,因此它依赖于实现。我想,如果你只关心一个编译器,那么可能是一个内部模板,可以为您提供类型,但我怀疑调用它会使语法比使用
decltype
更短。并且您必须提供一个类型作为向量的模板参数。您可以将
auto
粘贴在那里或类似的内容。您还可以动态分配
bind
o对象和具有
向量
void*
,因为您必须回溯到正确的类型才能调用
操作符()