4

I am confused about the use of boost::ref. I dont understand why any one would want to do the following -

void f(int x)
{
    cout << x<<endl;
    x++;
}

int main(int argc, char *argv[])
{
    int aaa=2;
    f(boost::ref(aaa));
    cout << aaa<<endl;
    exit(0);
}

What is the use of passing a ref to a function. I can always pass by value instead. Also it not that the ref is actually passed. In the above the value of aaa in main remains 2 only.

Where exactly is boost ref useful?

Is it possible to use boost::ref in this scenario. I want to pass iterator refernce to std::sort function. normally the sort works on iterator copies - will boost::ref make it work for references also? (without any changes to std::sort)

Useless
  • 64,155
  • 6
  • 88
  • 132
Rohit
  • 371
  • 4
  • 18

1 Answers1

17

I dont understand why any one would want to do the following

They wouldn't. That's not what boost::ref (or these days std::ref) is for. If a function takes an argument by value, then there's no way to force it to take it by reference instead.

Where exactly is boost ref useful?

It can be used to make a function template act as if it takes an argument by reference, by instantiating the template for the reference (wrapper) type, rather than the value type:

template <typename T>
void f(T x) {++x;}

f(aaa);      cout << aaa << endl; // increments a copy: prints 0
f(ref(aaa)); cout << aaa << endl; // increments "a" itself: prints 1

A common specific use is for binding arguments to functions:

void f(int & x) {++x;}

int aaa = 0;
auto byval = bind(f, aaa);      // binds a copy
auto byref = bind(f, ref(aaa)); // binds a reference

byval(); cout << aaa << endl;   // increments a copy: prints 0
byref(); cout << aaa << endl;   // increments "a" itself: prints 1

Is it possible to use boost:;ref in this scenario. I want to pass iterator refernce to std::sort function. normally the sort works on iterator copies - will boost::ref make it work for references also?

No; the reference wrapper doesn't meet the iterator requirements, so you can't use it in standard algorithms. If you could, then many algorithms would go horribly wrong if they needed to make independent copies of iterators (as many, including most sort implementations, must do).

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • oki understand but can you explain how this works. template void f(_T x) { x++; } int main(int argc, char *argv[]) { int aaa=2; f>(boost::ref(aaa)); cout << aaa<>aaa; exit(0); } The value printed is 3. Dont understand how x i function became a refernce ? – Rohit May 09 '13 at 12:16
  • @user2161003: By explicitly writing out the `reference_wrapper` part (which isn't necessary), you've answered your own question: the template is instantiated with a "reference wrapper" type (returned by `ref()`), and that acts like a reference to the variable it wraps. – Mike Seymour May 09 '13 at 12:28
  • 1
    but how? and object of reference_wrapper does not have ++ operator. how is x++ working? – Rohit May 09 '13 at 12:30
  • @user2161003: Using its conversion operator, `operator T&()`, to convert to a real reference when needed. So `++x` becomes `++(x.operator int&())` and increments `aaa`. – Mike Seymour May 09 '13 at 12:36
  • ok - i had a hunch that it is beacuse of this. But what i dont understand are the rules of calling this type case operator. If x is a refernce_wrapper the i do understand that the operator will get called if i do something like this - int& a = x. but just doing x++ is also invoking the operator. strange!!! – Rohit May 09 '13 at 12:45