Bug 79318 (cwg1726) - [DR 1726] conversion member function preceded with & is not marked as error
Summary: [DR 1726] conversion member function preceded with & is not marked as error
Status: NEW
Alias: cwg1726
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 6.3.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid
Depends on:
Blocks: c++-core-issues
  Show dependency treegraph
 
Reported: 2017-02-01 07:04 UTC by John Simon
Modified: 2022-03-15 16:50 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-12-03 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description John Simon 2017-02-01 07:04:47 UTC
See example and discussion on https://stackoverflow.com/questions/41954449/what-does-gccs-conversion-function-operator-int-do

Conversion member function declaration like

&operator int ();

does not produce a warning or error and is interpreted as

operator int& ();

by GCC. It fails on e.g. CLANG. 
Usage of & in front of operator seems not to be covered by the language definition.

Verified up to gcc 6.3.
Comment 1 John Simon 2017-02-01 07:44:11 UTC
Full example:

#include <iostream>

using namespace std;


class Foo
{
  private:
    int a;
  public:
    Foo():a(0){};
    &operator int() {return a;};  // this fails with Clang    
};


int main()
{
  Foo foo;
  
  int x = foo;
  
  cout << x << endl;  
    
}
Comment 2 Jakub Jelinek 2017-02-01 09:25:30 UTC
Smaller testcase:
struct A
{
  int a;
  A () : a(0) {};
  &operator int () { return a; };
};

int
bar ()
{
  A a;
  return a;
}

clang++ indeed reports:
pr79318.C:5:3: error: cannot specify any part of a return type in the declaration of a conversion function; put the complete type after 'operator'
  &operator int () { return a; };
  ^
                &

[dcl.dcl]/12 indeed requires that conversion functions are declared with nodeclspec-function-declaration.
Comment 3 Jonathan Wakely 2017-02-01 16:06:01 UTC
This seems to be http://wg21.link/cwg1726
Comment 4 Andrew Pinski 2021-12-03 06:39:26 UTC
Confirmed.
The defect report has been in drafting since 2013.  Maybe it was forgotten about.
I assume it is supposed to be rejected too.
Comment 5 Marek Polacek 2022-03-15 16:50:39 UTC
CWG 1726 is now a DR.