Bug 32658 - Supposedly illegal conversion compiles without errors
Summary: Supposedly illegal conversion compiles without errors
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.3.0
: P3 normal
Target Milestone: ---
Assignee: Nathan Sidwell
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2007-07-06 21:43 UTC by Aristid Breitkreuz
Modified: 2016-10-10 13:05 UTC (History)
1 user (show)

See Also:
Host: x86_64-linux-gnu
Target: x86_64-linux-gnu
Build: x86_64-linux-gnu
Known to work:
Known to fail:
Last reconfirmed: 2007-08-01 19:42:14


Attachments
File that compiles unexpectedly, without warnings (no #includes) (95 bytes, text/plain)
2007-07-06 21:44 UTC, Aristid Breitkreuz
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Aristid Breitkreuz 2007-07-06 21:43:44 UTC
xyz::operator xyz &() gets called implicitly. I'm told that it shouldn't.

Is this intended behaviour and will it stay?

$ LC_ALL=C gcc -v
Using built-in specs.
Target: x86_64-unknown-linux-gnu
Configured with: ../gcc-4.3-20070608/configure --prefix=/opt/unstable/gcc-4.3-20070608 --enable-languages=c,c++ --disable-multilib
Thread model: posix
gcc version 4.3.0 20070608 (experimental)
Comment 1 Aristid Breitkreuz 2007-07-06 21:44:44 UTC
Created attachment 13857 [details]
File that compiles unexpectedly, without warnings (no #includes)
Comment 2 Andrew Pinski 2007-07-07 00:00:15 UTC
And why do you think it is not?
I don't have access to my copy of the standard but I think this is valid thing to do.
Comment 3 Aristid Breitkreuz 2007-07-07 01:46:19 UTC
I don't have a copy of the standard at all, unfortunately, but I was referred to [12.3.2/1 p.6]. If I understood correctly, the problem being that implicit conversions shall not take place for the class or a base class of the type.
Comment 4 Aristid Breitkreuz 2007-07-09 12:39:14 UTC
I should note that I do NOT want to see this bug fixed. I would prefer to hear that you won't "fix" it at all. So I can exploit this behavior as there is no standards-compliant way of achieving the same results.
Comment 5 Nathan Sidwell 2007-08-01 19:42:14 UTC
The standard is unclear about exactly why this is ill-defined.  Does the conversion operator take part in overload resolution (and then be rejected, if it is selected), or is it never entered into the overload set.

I shall query CWG.
Comment 6 James Widman 2007-08-02 15:56:44 UTC
(In reply to comment #4)
> I should note that I do NOT want to see this bug fixed. I would prefer to hear
> that you won't "fix" it at all. So I can exploit this behavior as there is no
> standards-compliant way of achieving the same results.

Is it possible that rvalue references will give you an alternative for the desired effect?  See the relevant papers linked to from here:

http://open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2291.html
Comment 7 Aristid Breitkreuz 2007-08-02 16:34:47 UTC
(In reply to comment #6)
> (In reply to comment #4)
> > I should note that I do NOT want to see this bug fixed. I would prefer to hear
> > that you won't "fix" it at all. So I can exploit this behavior as there is no
> > standards-compliant way of achieving the same results.
> 
> Is it possible that rvalue references will give you an alternative for the
> desired effect?  See the relevant papers linked to from here:
> 
> http://open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2291.html
> 

This would mean that instead of A::A(A &), I wrote A::A(A &&) and passing temporaries would automatically work?

(Not sure if I correctly understand the r-value reference proposal.)

Comment 8 James Widman 2007-08-02 17:07:31 UTC
(In reply to comment #7)
> (In reply to comment #6) 
> > Is it possible that rvalue references will give you an alternative for the
> > desired effect?  See the relevant papers linked to from here:
> > 
> > http://open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2291.html
> > 
> 
> This would mean that instead of A::A(A &), I wrote A::A(A &&) and passing
> temporaries would automatically work?

That's correct.  Rvalues  would bind directly to the 'A&&' parameter; you could even have two ctors:
struct A {
 A(const A&);   // copy ctor
 A(A&&);          // move ctor
};
...and when you initialize an 'A' with an rvalue, overload resolution will select the move ctor.
Comment 9 James Widman 2007-08-02 17:17:01 UTC
See also: 

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

So it seems you should be able to play with it now.
Comment 10 Aristid Breitkreuz 2007-08-02 18:39:40 UTC
(In reply to comment #9)
> See also: 
> 
> http://gcc.gnu.org/bugzilla/show_bug.cgi?id=29939
> 
> So it seems you should be able to play with it now.
> 

Unfortunately, I have to support older GCC version (like 4.0 and 4.1) for now.
Comment 11 Paolo Carlini 2012-08-22 20:56:01 UTC
Nathan, shall we close this?
Comment 12 Nathan Sidwell 2016-10-10 13:05:39 UTC
This appears resolved.

32658.ii: In function 'void bar()':
32658.ii:12:6: error: invalid initialization of non-const reference of type 'xyz&' from an rvalue of type 'xyz'
  foo(xyz());
      ^~~~~
32658.ii:9:6: note:   initializing argument 1 of 'void foo(xyz&)'
 void foo(xyz &);
      ^~~
which looks right to me.  Now we have rvalue refs, one can DTRT (doesn't help Aristid thoug).