Re: Why you should never use a const& parameter to initialize a const& member variable!
Martin B. wrote:
myFAQ 0815 - Why you should never use a const& parameter to initialize a
const& member variable!
Today, once again, I shot myself in the foot. I thought I'd share this.
Rule: You must never use a const-reference parameter to a constructor to
initialize a const-reference member-variable.
Reason: const& parameters bind to temporaries. You do not want to track
temporaries!
Solution: Use a const* parameter
If you want a const& member variable in a class to reference something,
then it has to be initialized in the ctor. But you must not use a const&
parameter to the ctor to initialize the member, because this parameter
would bind to a temporary and then you would be tracking the temporary
instead of the original value.
I found this in
The C++ Programming Language by Stroustrup, Section 5.5 References
"The initializer for a plain T& must be an lvalue of type T.
The initializer for a const T& need not be an lvalue or even of type T.
In such cases,
[1] first, implicit type conversion to T is applied if necessary;
[2] then, the resulting value is placed in a temporary variable of type
T; and
[3] finally, this temporary is used as the value of the initializer.
Consider: const double& cdr = 1;
The interpretation might be:
double temp = double(1);
const double& cdr = temp;
" (end of quote)
The problem is that const has double meanings. Sometimes it means
read-only and sometimes it means constant value. In Martin's example
const means read-only, but to the compiler it means will-never-change,
hence it it freely makes a copy!
Example demonstrating the issue:
--------------------------------
#include <iostream>
using namespace std;
class Bad {
int const& tracker_;
public:
explicit Bad(int const& to_track)
: tracker_(to_track)
{ }
void print() {
cout << "bad tracker_ is: " << tracker_ << endl;
}
};
class Better {
int const& tracker_;
public:
explicit Better(int const* to_track)
: tracker_(*to_track)
{ }
void print() {
cout << "better tracker_ is: " << tracker_ << endl;
}
};
int f() {
static int i = 1;
i += 5;
return i;
}
int main()
{
int t = 100;
char c = 32;
Bad a1( f() ); // compiles: bad
// Better b1( &(f()) ); - compiler error: good
Bad a2( c ); // compiles: bad
// Better b2( &c ); - compiler error: good
Bad a3( t );
Better b3( &t );
t = 166;
c = 64;
t = f();
a1.print(); // May crash or just print 6 (or whatever)
// b1.print();
a2.print(); // May crash or just print 32
// b2.print();
a3.print(); // OK
b3.print(); // OK
return 0;
}
--------------------------------
br,
Martin
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]