Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why cv-qualivied reference is not allowed?

16 views
Skip to first unread message

ZMZ

unread,
Nov 23, 2010, 4:58:47 AM11/23/10
to
I tried to search google but couldn't find anything useful. Everybody
is talking about the rule, but nobody can tell why.


From C++ Standard 2003, 8.3.2/1

Cv-qualified references are ill-formed except when the cv-qualifiers
are introduced through the use of a typedef (7.1.3) or of a template
type argument (14.3), in which case the cv-qualifiers are ignored.

Meaning that,
int b;
int &const a = b; //ill-formed

typedef int &RefType;
const RefType c = b; //ok, but const ignored

I am very curious about the reason behind such rule. Since reference
cannot be re-binded later on, which means that a reference is already
a "const" value, I can't see any harm to add const-qualifier to a
reference. What's the reason to ban such cv-qualified reference?

On the other hand, why cv-qualified reference via typedef or template
are allowed?

Thanks in advance.

gwowen

unread,
Nov 23, 2010, 5:20:12 AM11/23/10
to
On Nov 23, 9:58 am, ZMZ <zhangmin...@gmail.com> wrote:
> Cv-qualified references are ill-formed except when the cv-qualifiers
> are introduced through the use of a typedef (7.1.3) or of a template
> type argument (14.3), in which case the cv-qualifiers are ignored.

References cannot be reseated so they're implicitly const, and cannot
be meaningfully volatile. Note that the referred to type can have CV
qualifiers:
So :

int main(int,char**)
{
int foo = 7;
const int& foo_ref = foo;
// This is fine -- foo cannot be modified through foo_ref
volatile int& foo_ref2 = foo;
// This is fine -- reads and writes to foo through foo_ref2
// will not be optimised away
}

int main(int,char**)
{
int bar = 7;
int & const bar_ref = bar;
// This is meaningless as bar_ref could not be made to
// refer to a different int anyway
}

ZMZ

unread,
Nov 23, 2010, 7:28:21 AM11/23/10
to

I just got the answer from a paper published in 1993 N0342, section 7,

7. Volatile Reference Types

Consider the following:
struct A {
A();
~A();
int& r;
};
volatile A va;

Now, what is the type of the expression va.r ? Does it have any
special semantics? This affects Tom Plum’s §5.2.4
rewrite.

Also, is the following allowed:
int& volatile vr;
?
Can it be constructed with typedefs? A similar question is posed in
§8.2.2.

In order to avoid dealing with these unimportant but possibly tricky
constructs, I suggest that we ban volatile references
or volatile instances of classes containing references.


-----------

There's also a paper in 1994 N0403 that was against such idea. But
"unfortunately" the later idea wasn't accepted and C++ was left with a
set of inconsistent rules that is hard for beginners to grasp.

One idea in N0403 is as follows, this is the reason that template is
allowed to make const or volatile reference I think.

The following example shows that templates can sometimes cause const-
qualified references to be introduced. Disallowing const-qualified
references in the language forces the template class to be rewritten
just to support the use of reference types.

template<class T> class C {
const T a;
};
typedef int &intr;
C<intr> x;

Notice that “C<intr>::a” is of type “int & const”, so this whole
program would be ill-formed if const-qualified references were
disallowed. Where did the poor user go wrong?

0 new messages