🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.bug
Article #1287

G++ Problem

#1287
From: Jeff Gehlhaar
Date: Wed, 16 Dec 1992 02:19
61 lines
2959 bytes
This suggestion was generated using gcc version 2.3.2 and libg++ 2.3 on a
sparc machine.

// Operator << should take (const void *) instead of (void *)

// I'm trying to write some code that will print an ID for a class.  When
// compiled with debugging on, an ID is kept in the class and is the value
// used.  However, when compiled with debugging off, the it would be nice
// to be able to dummy up a half-way reasonable ID.

// The obvious candidate is to return the address of the class entity.
// But this can't be done without generating errors or warnings.  Here's
// a snippet with the three candidates for concocting an ID.

#include <iostream.h>
class Class
{
public:
        void *ID1() { return this; }
        void *ID2() const { return this; }
        const void *ID3() const { return this; }
};

// These all break down in the presence of a constant object:

void demo(const Class *one)
{
                // The first try yields an error.  This is a correct
                // diagnosis; it's really illegal.
        cout << one->ID1();
                // non-const member function `void * Class::ID1 ()'
                // called for const object at this point in file

                // So, back to the drawing board; we say it is a const
                // member function.  This complains about converting
                // the const this pointer into a non-const void* pointer.
        cout << one->ID2();
                // In method `void * Class::ID2 ()const ':
                // return of non-`const *' pointer from `const *'

                // Well, that's reasonable; we should make the result a
                // const void* pointer.  Unfortunately, this gets a
                // warning because ostream& operator<< takes a (void *)
                // instead of a (const void *).
        cout << one->ID3();
                // argument passing of non-`const *' pointer from `const *'
}

// The easiest fix would be to change the library to take a (const void *)
// and depend upon the trivial promotion to match it---note that the other
// similar functions that take a pointer take a const pointer.

// The alternative would be to change the compiler to not generate a
// diagnostic when a non-const pointer is cast to void*.

// I can't find anything in the ARM or either of the two reference books
// I have that suggests which is appropriate.  For the time being, I'm
// using ID3 and providing my own operator<< that takes a (const void *).
// It works, but it's a hack, and I'm not fond of it....

// -- Greg
Technical Details
Message-ID: <9212161625.AA24104@harvey>
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!qualcomm.com!jbg

Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug
Path: arc1-ca.pugleaf.net!sparky!uunet!cis.ohio-state.edu!qualcomm.com!jbg
From: jbg@qualcomm.com (Jeff Gehlhaar)
Subject: G++ Problem
Message-ID: <9212161625.AA24104@harvey>
Sender: gnulists@ai.mit.edu
Organization: GNUs Not Usenet
Distribution: gnu
Date: Wed, 16 Dec 1992 02:19:43 GMT
Approved: bug-g++@prep.ai.mit.edu
Lines: 64