什么是boost :: bind的返回类型? [英] What is the return type of boost::bind?
问题描述
我想将一个函数的binder保存到一个变量中,通过利用它的操作符重载设施在下面的代码中重复使用它。这里是实际做我想要的代码:
I want to save the "binder" of a function to a variable, to use it repetitively in the following code by exploiting its operator overloading facilities. Here is the code that actually does what I want:
#include <boost/bind.hpp>
#include <vector>
#include <algorithm>
#include <iostream>
class X
{
int n;
public:
X(int i):n(i){}
int GetN(){return n;}
};
int main()
{
using namespace std;
using namespace boost;
X arr[] = {X(13),X(-13),X(42),X(13),X(-42)};
vector<X> vec(arr,arr+sizeof(arr)/sizeof(X));
_bi::bind_t<int, _mfi::mf0<int, X>, _bi::list1<arg<1> > > bindGetN = bind(&X::GetN,_1);
cout << "With n =13 : "
<< count_if(vec.begin(),vec.end(),bindGetN == 13)
<< "\nWith |n|=13 : "
<< count_if(vec.begin(),vec.end(),bindGetN == 13 || bindGetN == -13)
<< "\nWith |n|=42 : "
<< count_if(vec.begin(),vec.end(),bindGetN == 42 || bindGetN == -42)
<< "\n";
return 0;
}
令我烦恼的是,当然是:
What bothers me is, of course, the line:
bi::bind_t<int, _mfi::mf0<int, X>, _bi::list1<arg<1> > > bindGetN = bind(&X::GetN,_1);
我只是故意产生类型错误并分析错误消息,这当然不是一个好的方法去。有没有办法获取bindGetN的类型?或者,也许有不同的方法来产生类似的功能?
I've obtained the type just by deliberately making a type error and analysing the error message. That is certainly not a good way to go. Is there a way to obtain the type for the "bindGetN"? Or, maybe there are different ways to produce similar functionality?
编辑:我忘了提及,标准建议使用 function
在这种情况下不工作 - 因为我想让我的操作符重载。
I forgot to mention that the, so to say, "standard" suggestion to use function
is not working in this case -- because I'd like to have my operator overloading.
推荐答案
简单的答案是:你不需要知道(实现定义)。
它是一个绑定表达式( std :: tr1 :: is_bind_expression< T> :: value
对于实际类型产生true)。
The short answer is: you don't need to know (implementation defined).
It is a bind expression (std::tr1::is_bind_expression<T>::value
yields true for the actual type).
查看
-
std :: tr1 :: function< / code>
- BOOST_AUTO ()
- c ++ 0x'auto'关键字(类型推论)
- 它可以帮助您进一步移动
- 它可以帮助您进一步移动
std::tr1::function<>
- BOOST_AUTO()
- c++0x 'auto' keywords (Type Inference)
- it's close cousing
decltype()
can help you move further
- it's close cousing
1。
1.
std::tr1::function<int> f; // can be assigned from a function pointer, a bind_expression, a function object etc
int realfunc();
int realfunc2(int a);
f = &realfunc;
int dummy;
f = tr1::bind(&realfunc2, dummy);
2。
BOOST_AUTO支持语法c ++ 0x auto没有编译器c ++ 0x支持:
2.
BOOST_AUTO() aims to support the semantics of c++0x auto without compiler c++0x support:
BOOST_AUTO(f,boost::bind(&T::some_complicated_method, _3, _2, "woah", _2));
3。
与编译器支持:
3.
Essentially the same but with compiler support:
template <class T> struct DoWork { /* ... */ };
auto f = boost::bind(&T::some_complicated_method, _3, _2, "woah", _2));
DoWork<decltype(T)> work_on_it(f); // of course, using a factory would be _fine_
注意,auto可能是为这种类型发明的的情况:实际类型是你不想知道,并且可能因编译器/平台/库而异
Note that auto has probably been invented for this kind of situation: the actual type is a 'you don't want to know' and may vary across compilers/platforms/libraries
这篇关于什么是boost :: bind的返回类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!