Topic: void test(const A)


Author: "Yaroslav Mironov" <yaro@nevacom.spb.ru>
Date: Mon, 16 Apr 2001 17:23:25 GMT
Raw View
Hello,

I've got a little question about the topmost const qualifier in function
parameters (one that is stripped when determining function type). Suppose,
the qualifier is absent in the declaration of a function but is present in
the definition. I thought that it is the presence or absence of the const
qualifier in the _definition_ of a fucntion that sets whether the parameter
is treated as const in the body of the function or not. But the reality with
BCC55 turned different:


#include <iostream>

struct A
{
void m() const {std::cout << "Const\n";}
void m() {std::cout << "Non-const\n";}
};


// that's how I declare test
void test(const A, A);

// that's how I define test
void test(A x, const A y)
{
x.m();
y.m();
}

int
main()
{
test(A(), A());
}


Surprisingly, this piece of code being compiled with BCC55 produces the
following output:

Const
Non-const

That is, x is treated as const and y as non-const. Am I missing something?

Yaroslav

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Valentin.Bonnard@free.fr (Valentin Bonnard)
Date: Mon, 16 Apr 2001 18:15:05 GMT
Raw View
> I thought that it is the presence or absence of the const
> qualifier in the _definition_ of a fucntion that sets whether the
> parameter is treated as const in the body of the function or not.
> But the reality with BCC55 turned different:

This compiler is broken.

--
Valentin Bonnard

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: Ron Natalie <ron@stop.mail-abuse.org>
Date: Mon, 16 Apr 2001 18:44:21 GMT
Raw View

Yaroslav Mironov wrote:

> Surprisingly, this piece of code being compiled with BCC55 produces the
> following output:
>
> Const
> Non-const
>
> That is, x is treated as const and y as non-const. Am I missing something?

The cv-qualifier on the parameters is not supposed to make any difference
in the function type, it is purely to describe the behavior of the parameter
inside the function.   That would lend you to believe that it should print
const then non-const.

However, I'm not sure your program is well-formed.  While as far as the
language is concerned the type of "void test(const A, A)" and "void test(A,
const A)" are the same (void ()(A, A)), the standard says that all declarations
should have matching parameter types.  The stripping of const rule only
seems to apply with the computation of the function type itself.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: comeau@panix.com (Greg Comeau)
Date: Mon, 16 Apr 2001 22:02:20 GMT
Raw View
In article <066601c0c5ed$b0d2cd20$9e00000a@yaroslav.nevacom.spb.ru>,
Yaroslav Mironov <yaro@nevacom.spb.ru> wrote:
>I've got a little question about the topmost const qualifier in function
>parameters (one that is stripped when determining function type). Suppose,
>the qualifier is absent in the declaration of a function but is present in
>the definition. I thought that it is the presence or absence of the const
>qualifier in the _definition_ of a fucntion that sets whether the parameter
>is treated as const in the body of the function or not.

You are correct.

>But the reality with
>BCC55 turned different:
>
>#include <iostream>
>struct A
>{
>void m() const {std::cout << "Const\n";}
>void m() {std::cout << "Non-const\n";}
>};
>
>// that's how I declare test
>void test(const A, A);
>// that's how I define test
>void test(A x, const A y)
>{
>x.m();
>y.m();
>}
>
>int main()
>{
>test(A(), A());
>}
>
>Surprisingly, this piece of code being compiled with BCC55 produces the
>following output:
>
>Const
>Non-const

That's wrong.

>That is, x is treated as const and y as non-const. Am I missing something?

Nope, you got it right.
--
Greg Comeau                 Comeau C/C++ 4.2.45.2 "it's so close"
ONLINE COMPILER ==>         http://www.comeaucomputing.com/tryitout
NEW: Try out libcomo!       NEW: Try out our C99 mode!
comeau@comeaucomputing.com  http://www.comeaucomputing.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]





Author: "Yaroslav Mironov" <yaro@nevacom.spb.ru>
Date: Tue, 17 Apr 2001 15:40:26 GMT
Raw View
Ron Natalie wrote <3ADB2EB5.DD6655FD@stop.mail-abuse.org> ...
>However, I'm not sure your program is well-formed.  While as far as the
>language is concerned the type of "void test(const A, A)" and "void test(A,
>const A)" are the same (void ()(A, A)), the standard says that all
declarations
>should have matching parameter types.  The stripping of const rule only
>seems to apply with the computation of the function type itself.

In fact I wondered too, whether the requirements of 8.3.5/3:

<<
...All declarations for a function with a given parameter list shall agree
exactly both in the type of the value returned and in the number and type of
parameters...

...The type of each parameter is determined from its own decl-specifier-seq
and declarator...

...After producing the list of parameter types, several transformations take
place upon these types to determine the function type. Any cv-qualifier
modifying a parameter type is deleted...
>>

That is, cv-qualifiers are removed only after producing the list of
parameter types (to determine the type of a function), not when determining
the type of each parameter, therefore two functions with different
cv-qualifiers on parameters would not agree "in the number and type of
parameters".

So, whether the requirment of 8.3.5/3 agree with the explanation and
examples in 13.1/3:

<quote>
Parameter declarations that differ only in the presence or absence of
const and/or volatile are equivalent.
That is, the const and volatile type-specifiers for each parameter type are
ignored when determining which function is being declared, defined, or
called.
[Example:

typedef const int cInt;

int f (int);
int f (const int);    // redeclaration of f(int)
int f (int) { ... }   // definition of f(int)
int f (cInt) { ... }  // error: redefinition of f(int)

--- end example]
</quote>

Yaroslav

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html                ]