🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

conversion method never called

#1352
From: Paul Stravers
Date: Thu, 07 Jan 1993 19:08
47 lines
2251 bytes
This reports a bug in g++ version 2.3.1 and 2.3.3.

If you specify a conversion method that converts a class object to A REFERENCE
to something else, then g++ never generates code that calls this conversion
method. The following example defines a class "test" that has a conversion
operator int&() that is never called, even if we specify a typecast from "test"
to "int&" (as is done in the second line of main()):
------------------------------------------------------------------------------
#include <stdio.h>

class test
{
   double d;
   int    i;
public:
   test(double dd,int ii) {d=dd; i=ii;} // constructor
   operator int&()        {return i;} // define a conversion from test to int&
   int& geti()            {return i;} // same thing, but different
};

main()
{
   test t(3.14, 5);  // Create an object t of class "test"
   int x = (int&)t;  // This should call operator int&() but it does not ...
   int y = t.geti(); // x and y should both be 5 ...
   printf("%d\n%d\n", x, y);
}
------------------------------------------------------------------------------
The output (on a sparc station) of this program is:

    % gcc test.C
    % a.out
    1074339512
    5

The same problem appears on a HP snake. The problem disappears if we specify
any other conversion method for the class test that is not a conversion to a
reference to something. For instance, "operator int ()" works fine, but
"operator int& ()" does not.

Paul Stravers
-- 
Delft University of Technology      |  email stravers@donau.et.tudelft.nl
Faculty of Electrical Engineering   |  fax +31 15 786190
Mekelweg 4                          |  ph. +31 15 786280
2628 CD   Delft                     |
the Netherlands                     |
Technical Details
Message-ID: <9301071708.AA03432@muresh.et.tudelft.nl>
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!muresh.et.tudelft.nl!stravers

Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.bug
Path: arc1-ca.pugleaf.net!sparky!uunet!zaphod.mps.ohio-state.edu!cis.ohio-state.edu!muresh.et.tudelft.nl!stravers
From: stravers@muresh.et.tudelft.nl (Paul Stravers)
Subject: conversion method never called
Message-ID: <9301071708.AA03432@muresh.et.tudelft.nl>
Sender: gnulists@ai.mit.edu
Reply-To: Paul Stravers <stravers@donau.et.tudelft.nl>
Organization: GNUs Not Usenet
Distribution: gnu
Date: Thu, 7 Jan 1993 19:08:33 GMT
Approved: bug-g++@prep.ai.mit.edu
Lines: 48