public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/31419]  New: template user defined conversion operator instantiated for conversion to self
@ 2007-04-01 13:26 james dot kanze at gmail dot com
  2007-04-01 14:39 ` [Bug c++/31419] " fang at csl dot cornell dot edu
                   ` (8 more replies)
  0 siblings, 9 replies; 10+ messages in thread
From: james dot kanze at gmail dot com @ 2007-04-01 13:26 UTC (permalink / raw)
  To: gcc-bugs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 1933 bytes --]

The following code does not compile:
------------------templcvt.cc----------------
extern int f( int ) ;

class B
{
public:
    template< typename T >
        operator T const& () const
    {
        return f( 42 ) ;
    }
} ;

B
g()
{
    return B() ;
}
---------------------------------------------
When invoking "g++ -c templcvt.cc", the compiler outputs the
following error messages:

    templcvt.cc: In member function 'B::operator const T&() const [with T =
B]':
    templcvt.cc:24:   instantiated from here
    templcvt.cc:17: error: invalid initialization of reference of type 'const
B&' from expression of type 'int'

Apparently, it is instantiating the template conversion operator
in order to bind the temporary object to the B const& argument
of the (implicit) copy constructor.  This dispite §12.3.2.1 "A
conversion function is never used to convert a (possibly
cv-qualified) object to the (possibly cv-qualified) same object
type (or a reference to it),[...]" (and the fact, of course,
that a function template should only be instantiated if the
function is used).

There's a simple work-around: just define an explicit:

    operator B const&() const { return *this ; }

as well.  But this shouldn't be necessary.

(See also the discussion in comp.std.c++:
http://groups.google.com/group/comp.std.c++/browse_frm/thread/32139b794c433839/#)


-- 
           Summary: template user defined conversion operator instantiated
                    for conversion to self
           Product: gcc
           Version: 4.1.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: james dot kanze at gmail dot com
 GCC build triplet: x86_64-unknown-linux-gnu
  GCC host triplet: x86_64-unknown-linux-gnu
GCC target triplet: x86_64-unknown-linux-gnu


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31419


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2007-09-13 16:05 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-04-01 13:26 [Bug c++/31419] New: template user defined conversion operator instantiated for conversion to self james dot kanze at gmail dot com
2007-04-01 14:39 ` [Bug c++/31419] " fang at csl dot cornell dot edu
2007-04-01 14:42 ` james dot kanze at gmail dot com
2007-04-01 15:57 ` fang at csl dot cornell dot edu
2007-04-09 14:35 ` [Bug c++/31419] [4.1/4.2/4.3 regression] " reichelt at gcc dot gnu dot org
2007-04-15 22:13 ` mmitchel at gcc dot gnu dot org
2007-04-23 16:03 ` janis at gcc dot gnu dot org
2007-09-04 14:06 ` jason at gcc dot gnu dot org
2007-09-04 20:18 ` jason at gcc dot gnu dot org
2007-09-13 16:05 ` jason at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).