0

People tend to say pass by (const) reference if you want speed, to be honest I rarely use const reference on methods I create just because of speed, I only make a reference when I need it, for example to give a value to a variable, as in this SSCE

void Do( int& iref )
{
    a = 2;
}

{
    int a = 0;
    Do( a );
}

The question actually is, if the object was created on the function call, if will it also be passed as a reference, how does that works?

void Add( CString& strPath )
{
    g_Path.push_back( strPath );
}

for( std::string line; getline( input, line ); )
{
    Add( CString( line.c_str() ) ); //object created on function call
}
Vinícius
  • 15,498
  • 3
  • 29
  • 53
  • 4
    People tend to say a lot, especially when they want to impress girls or get a promotion. If you [want speed, pass by value](http://cpp-next.com/archive/2009/08/want-speed-pass-by-value/), though. – Kerrek SB Oct 13 '12 at 15:38
  • related: http://stackoverflow.com/questions/1565600/how-come-a-non-const-reference-cannot-bind-to-a-temporary-object – Vaughn Cato Oct 13 '12 at 15:39
  • @KerrekSB, I guess Scott Meyers was single back then...hehe! – Vinícius Oct 13 '12 at 15:45
  • 4
    I don't know. This whole "pass by X if you want speed" things always sound wrong to me. The two do different things; why would the choice be about speed? If you need a copy, you have no choice but to make a copy anyway; passing by reference won't make that any faster. If you don't need a copy, why are you copying? – R. Martinho Fernandes Oct 13 '12 at 15:47
  • 1
    @R.MartinhoFernandes: That's the sanest thing anyone has said on this topic in a long time. – Kerrek SB Oct 13 '12 at 15:48

1 Answers1

3

How does that work? It doesn't. The code doesn't compile, since temporary objects cannot bind to non-const lvalue references.

(Temporary objects can bind to const references, or to rvalue references, though.)

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • maybe I'm using an outdated compiler? Because it compiles without warnings /W3 – Vinícius Oct 13 '12 at 15:39
  • @ViniyoShouta: It depends. Please post the code of `CString`. I was assuming that it's a class name and thus there's a temporary object. It could of course be something different. – Kerrek SB Oct 13 '12 at 15:41
  • I'm using CString on a non MFC application using SL `atlstr.h` http://msdn.microsoft.com/en-us/library/aa314317%28v=vs.60%29.aspx (yes CString is a class) – Vinícius Oct 13 '12 at 15:43
  • @ViniyoShouta AFAIK MSVC allows that. – R. Martinho Fernandes Oct 13 '12 at 15:44
  • Can you try a simpler example without user-defined types? Put it on ideone or some online compiler website when you get it to work. It might be a bug in your compiler, or an extension. I'm assuming we're discussing C++ the language, not particular vendor variations. – Kerrek SB Oct 13 '12 at 15:45
  • Yes, it won't compile on Ideone, althought Microsoft vs2003 compiler doesn't think the same, http://ideone.com/9Q8XT, http://prntscr.com/hczpx – Vinícius Oct 13 '12 at 15:49
  • with /W4 I get a warning although http://msdn.microsoft.com/en-us/library/186yxbac%28v=vs.80%29.aspx – Vinícius Oct 13 '12 at 15:53