This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
Re: [PATCH] PR c++/80544 strip cv-quals from cast results
- From: Jonathan Wakely <jwakely at redhat dot com>
- To: Jakub Jelinek <jakub at redhat dot com>
- Cc: Andreas Schwab <schwab at linux-m68k dot org>, Jason Merrill <jason at redhat dot com>, gcc-patches List <gcc-patches at gcc dot gnu dot org>, Nathan Sidwell <nathan at acm dot org>
- Date: Thu, 25 May 2017 16:43:38 +0100
- Subject: Re: [PATCH] PR c++/80544 strip cv-quals from cast results
- Authentication-results: sourceware.org; auth=none
- Authentication-results: ext-mx06.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com
- Authentication-results: ext-mx06.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=jwakely at redhat dot com
- Dkim-filter: OpenDKIM Filter v2.11.0 mx1.redhat.com D5A612D9FC0
- Dmarc-filter: OpenDMARC Filter v1.3.2 mx1.redhat.com D5A612D9FC0
- References: <CADzB+2nMhFnDrqk1jbE3nDx7b1hukJuWB3J5pSt=dGXmceOVPA@mail.gmail.com> <20170523180046.GG12306@redhat.com> <CADzB+2=yocABQpVueGU4rTwLbTwn1f8wkT2FEUC4ZS0=Ai+oig@mail.gmail.com> <20170524142022.GJ12306@redhat.com> <m2inkp744p.fsf@linux-m68k.org> <20170525100700.GU12306@redhat.com> <20170525140242.GA12306@redhat.com> <20170525141119.GE8499@tucnak> <20170525145247.GB12306@redhat.com> <20170525145611.GH8499@tucnak>
On 25/05/17 16:56 +0200, Jakub Jelinek wrote:
On Thu, May 25, 2017 at 03:52:47PM +0100, Jonathan Wakely wrote:
I'd probably write that like this instead:
void
foo (const void *p)
{
typedef const int* ptr_type;
ptr_type const q = (ptr_type) p;
ptr_type const r = (ptr_type) p;
(void) q;
(void) r;
}
It names the type only once, not twice as in your example, and doesn't
need to use typeof to refer to that type again.
That was over-simplified, I meant cases where you actually don't know the
exact type, just use typeof to create a variable of such a type and
then want to cast something to that type.
The most obvious case I can think of where you don't know the type
would be the result of some expression (maybe involving overloaded
operators, or a call to an overloaded function). In that case:
typeof (expr) const q = (typeof (expr)) p;
Again, add the const to the variable definition, but not the cast.
or using a typedef again for clarity and to avoid the repetition:
typedef typeof (expr) the_type;
the_type const q = (the_type) p;
If the result of the expression is a scalar type then it won't be
const-qualified, so the_type isn't either, and the cast won't warn.
If the result of the expression is a class-type then it can be
const-qualified, and so the_type is also const-qualified, but the
warning isn't issued in such case.
All the realistic case I can think of expr won't be simply a variable
(because if you have a variable then it was already declared with some
type and you can refer to that type directly) so the original problem
where typeof(q) deduces a const-qualified type won't happen.
template<typename T> void f (const T& t, const void* p) {
// no need for typeof(t) because we can just use T
T* const q = (T*)p;
T* const r = (T*)p;
}
There's this case, which will warn if the type is a pointer or other
scalar:
auto const q = some_function (p);
typeof (q) r = (typeof (q)) p
But what you really care about is typeof (some_function (p)) not
typeof (q), which is just the 'expr' case above:
typedef typeof (some_function(p)) ptr_type;
ptr_type const q = some_function (p);
ptr_type const r = (ptr_type)p;
The warning is avoidable by using typeof on the expression that
determines the type, not on a variable that has a const-qualified
version of the type.