This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Questions about placement of qualifiers in member functions
- From: Ian Lance Taylor <ian at wasabisystems dot com>
- To: gcc at gcc dot gnu dot org
- Date: 16 Dec 2003 00:53:09 -0500
- Subject: Questions about placement of qualifiers in member functions
I have two questions about the placement of qualifiers in member
functions in C++.
I thought this was a logical thing to write:
class C { public: int (*f())() const; };
int foo(const C c) { return (*c.f())(); }
However, when I compile it, I get this:
/home/ian/foo4.cc: In function `int foo(C)':
/home/ian/foo4.cc:2: error: passing `const C' as `this' argument of `int (* C::f())()' discards qualifiers
This seems strange to me, because it seems like the `const' on the
first line is just being ignored.
I found out that I could write the declaration like this:
class C { public: int (*f() const)(); };
int foo(const C c) { return (*c.f())(); }
and it would compile without error.
So my questions are:
1) Is it correct that a member function qualifier should be written
within the declaration when the member function returns a pointer
to a function? Why not write the qualifer after the list of
parameters, as is normally done?
2) If the answer to question 1 is that this is correct, then why
doesn't g++ warn about the `const' when compiling C::f in the first
function above? That `const' appears to mean nothing, which to me
means that its appearance ought to be a syntax error. If not, then
what does it mean?
I can file a PR about this, but I thought I would first check to see
whether there is some aspect to this that I do not understand.
Ian