Bug 13452 - No error on invalid CV qualifier on member function returning function pointer
Summary: No error on invalid CV qualifier on member function returning function pointer
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.0
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: accepts-invalid, wrong-code
: 26143 37885 86652 (view as bug list)
Depends on:
Blocks:
 
Reported: 2003-12-19 18:57 UTC by Ian Lance Taylor
Modified: 2025-01-29 15:07 UTC (History)
7 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work:
Known to fail: 3.3, 4.5.3, 4.8.3, 4.9.3, 5.3.0, 6.3.0, 7.0
Last reconfirmed: 2025-01-29 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ian Lance Taylor 2003-12-19 18:57:51 UTC
This code is compiled without error in the current mainline:

class C { public: template<typename i> int (*f())() const; };

However, it now seems to me that there should be an error on the trailing const.
 Otherwise, I have no idea what the trailing const means.  It appears that
generating a const method (i.e., a method for which `this' is a pointer to
const) requires

class C { public: template<typename i> int (*f() const)(); };

This may be a misunderstanding on my part, but at least some people share my
misunderstanding; see PR c++/13447.
Comment 1 Andrew Pinski 2003-12-23 18:22:24 UTC
Confirmed but not a regression.
Comment 2 Rainer.Bensch 2004-01-12 16:10:49 UTC
(In reply to comment #0)
> However, it now seems to me that there should be an error on the trailing 
const.

No.

>  Otherwise, I have no idea what the trailing const means.  It appears that
> generating a const method (i.e., a method for which `this' is a pointer to
> const) requires
> class C { public: template<typename i> int (*f() const)(); };
> This may be a misunderstanding on my part, but at least some people share my
> misunderstanding; see PR c++/13447.

You declared a non const member function f() which returns a pointer to a const
member function. If the const is inside the parenteses, you declare a const 
member function f() which returns a pointer to a non const member function. 
Now, consider to place const at both...

Cheers, Rainer
Comment 3 Ian Lance Taylor 2004-01-12 16:27:31 UTC
Subject: Re:  No error on invalid (I think) C++ code

"Rainer dot Bensch at rsd dot rohde-schwarz dot com" <gcc-bugzilla@gcc.gnu.org> writes:

> You declared a non const member function f() which returns a pointer to a const
> member function. If the const is inside the parenteses, you declare a const 
> member function f() which returns a pointer to a non const member function. 
> Now, consider to place const at both...

I don't agree.  The function f() does not return a pointer to any sort
of member function.  It returns a simple pointer to function.

Consider this test case:

extern int bar();
class C { public: int (*f())() const; };
int (*C::f())() const { return bar; }

Right now this compiles without error.  But the `const' in the
declaration of C::f() is meaningless.  C::f() returns an ordinary
function.  I certainly can't declare `extern int bar() const'.

Conversely, this test case:

class C { public: int (*f())() const; int bar(); };
int (*C::f())() const { return C::bar; }

fails, with:

foo3.cc:2: error: argument of type `int (C::)()' does not match `int (*)()

Similarly, this one:

class C { public: int (*f())() const; int bar() const; };
int (*C::f())() const { return C::bar; }

fails with:

foo3.cc:2: error: argument of type `int (C::)() const' does not match `int (*)()'

(both these error messages are mildly bogus, since the case here is
not an argument mismatch, but a return mismatch, but that is a
separate issue).

So I still think that the trailing const is meaningless, and should
cause an error.

Ian
Comment 4 Ian Lance Taylor 2005-05-25 00:33:18 UTC
Just a note that this still fails to issue an error on mainline.
Comment 5 Paolo Carlini 2013-04-09 17:15:57 UTC
*** Bug 37885 has been marked as a duplicate of this bug. ***
Comment 6 Paolo Carlini 2013-06-16 16:14:04 UTC
*** Bug 26143 has been marked as a duplicate of this bug. ***
Comment 7 Martin Sebor 2017-01-11 23:04:04 UTC
The invalid test case from comment #0 is still accepted by the top of trunk (GCC 7.0).  Both Clang and Oracle Solaris Studio reject the program with a similar error:

"t.C", line 1: Error: Non-member function pointer cannot point to a const function.
Comment 9 Andrew Pinski 2021-12-06 05:58:12 UTC
*** Bug 86652 has been marked as a duplicate of this bug. ***
Comment 10 Marek Polacek 2025-01-29 15:06:27 UTC
Still no error.  I've updated https://gcc.gnu.org/projects/cxx-dr-status.html