Re: const reference typedef compile error:
On Jan 2, 7:11 pm, smith7005 <smith7...@yahoo.com> wrote:
Can someone please explain why the following fails to compile?
//----------------
typedef int value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
struct woot
{
//OK
const value_type& foo() const { return value_; };
//OK
const_reference bar() const { return value_; };
//ERR
const reference waldo() const { return value_; };
value_type value_;
};
int main()
{}
//----------------
This is the error in gcc 3.4.6:
typedef.cpp: In member function `value_type& woot::waldo() const':
typedef.cpp:16: error: invalid initialization of reference of type
'value_type&' from expression of type 'const value_type'
-Thanks!
--
[ Seehttp://www.gotw.ca/resources/clcm.htmfor info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
section [8.3.2] 'References' of the standard covers this situation.
[Perhaps someone else can throw a little insight...i make a lousy
lawyer]
It basicly says that if you have a declaration that looks like this:
typedef int& Ref;
const Ref r = 99; // is ill-formed
// non-const reference initialized with rvalue
such CV-qualified references are ill-formed *except* when these are
introduced through typedefines or a template type arguement, in which
case the qualifiers are ignored (1). The type of r is
reference_to_integer, not const_reference_to_integer.
Incidentally, You might consider doing it with a template. Since a
reference to your private parts makes sense, waldo() is no longer a
const member function. You can also test the statement (1) in the
standard about ignoring the const qualifier.
#include <iostream>
template < typename value_type >
struct test
{
private:
value_type value_;
typedef value_type& reference;
typedef const value_type& const_reference;
// read (1) above, const would be ignored
// typedef const reference& const_reference;
public:
// ctors
test(const value_type t) : value_(t) { }
test(const test& copy)
{
value_ = copy.value_;
}
// member functions
const value_type& foo() const { return value_; };
reference waldo() { return value_; }; // ERR
const_reference bar() const { return value_; };
};
int main()
{
test< int > instance(99);
std::cout << instance.foo() << std::endl;
std::cout << instance.bar() << std::endl;
int& ref = instance.waldo();
ref = 55;
std::cout << instance.waldo() << std::endl;
}
/*
99
99
55
*/
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]