0

Is there a way to remove the 'plumb' version of all of my functions, without the need to change the 'hit' line to the 'fixed'?

Yes my program works fine, but I think if is there a way to get ride from this version of all of my functions.

Keep in mind that int is not really int in my programs, but a type alias which can be object ( e.g. container_reference<std::array<double,4>> ) or reference ( e.g. std::array<double,4> & )

void func(int &&m) { cout << "rvalue: " << m << endl; }
void func(int  &m) { cout << "lvalue: "; func(std::move(m)); } // PLUMB!

int main() 
{
    int a = 5;
    func(a);    // HIT!
    func(std::move(a)); // FIXED!
    func(6);
    func(a + 5);
}
Chameleon
  • 1,804
  • 2
  • 15
  • 21
  • I feel like you don't really understand, what you are doing. So the question is what are you trying to do? Do you want your `func` to work with rvalues or with lvalues? – Mikhail Apr 06 '13 at 19:04
  • I want to work with both. But the body code is exactly the same. – Chameleon Apr 06 '13 at 19:05

1 Answers1

5

I'm having a bit of trouble understand exactly what you want, but this might be an option:

template<typename T>
void func(T &&m) {
  // ...
}

T&& has been dubbed "universal reference" as it will bind to both lvalues and rvalues due to reference collapsing rules.

Pubby
  • 51,882
  • 13
  • 139
  • 180
  • universal reference is available only on template parameters, right? so `template void func – Chameleon Apr 06 '13 at 19:09
  • @Chameleon Yes, it requires a deduced type. I accidentally had `auto&&`, but I meant `T&&` here. `auto&&` works elsewhere though, as it's deduced too. – Pubby Apr 06 '13 at 19:13