public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/11342] New: Method selection warning prevents compilation
@ 2003-06-27  1:06 dan at photon dot com
  2003-06-27  1:19 ` [Bug c++/11342] " dan at photon dot com
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: dan at photon dot com @ 2003-06-27  1:06 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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

           Summary: Method selection warning prevents compilation
           Product: gcc
           Version: 3.2
            Status: UNCONFIRMED
          Severity: critical
          Priority: P1
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dan at photon dot com
                CC: gcc-bugs at gcc dot gnu dot org

In certain cases, I have source code that generates the following warning:

test.cpp In member function 'xxxx':
test.cpp:xx choosing 'char& String::operator[](unsigned int)' over 'operator[] '
test.cpp:xx   because worst conversion for the former is better than worst 
   conversion for the latter

I've gotten a test case pruned down to the class with the offending methods
(char& String::operator[](unsigned int) and char String::operator[](unsigned
int) const). But if I strip out everything in the class except for the methods,
the warning goes away. I will keep at it to create a simple test case an attach
it later.


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

* [Bug c++/11342] Method selection warning prevents compilation
  2003-06-27  1:06 [Bug c++/11342] New: Method selection warning prevents compilation dan at photon dot com
@ 2003-06-27  1:19 ` dan at photon dot com
  2003-06-27  1:36 ` dan at photon dot com
  2003-06-27 14:11 ` bangerth at dealii dot org
  2 siblings, 0 replies; 4+ messages in thread
From: dan at photon dot com @ 2003-06-27  1:19 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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



------- Additional Comments From dan at photon dot com  2003-06-27 01:19 -------
Here's the test code:

class Foo
{
public:
  Foo();
  virtual ~Foo();
  operator const char*() const;

  char& operator[](unsigned i);
  char operator[](unsigned i) const;
};

void func()
{
  Foo s;
  char c = s[0];
}


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

* [Bug c++/11342] Method selection warning prevents compilation
  2003-06-27  1:06 [Bug c++/11342] New: Method selection warning prevents compilation dan at photon dot com
  2003-06-27  1:19 ` [Bug c++/11342] " dan at photon dot com
@ 2003-06-27  1:36 ` dan at photon dot com
  2003-06-27 14:11 ` bangerth at dealii dot org
  2 siblings, 0 replies; 4+ messages in thread
From: dan at photon dot com @ 2003-06-27  1:36 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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



------- Additional Comments From dan at photon dot com  2003-06-27 01:36 -------
The warning can be circumvented by explicitly calling the [] operator:

char c = s.operator[](0);

Which leads me to believe that gcc did not choose the char& Foo::operator[] like
it claimed, but instead chose the const char* cast operator, for why else would
it compile when the const char* cast operator is not present?


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

* [Bug c++/11342] Method selection warning prevents compilation
  2003-06-27  1:06 [Bug c++/11342] New: Method selection warning prevents compilation dan at photon dot com
  2003-06-27  1:19 ` [Bug c++/11342] " dan at photon dot com
  2003-06-27  1:36 ` dan at photon dot com
@ 2003-06-27 14:11 ` bangerth at dealii dot org
  2 siblings, 0 replies; 4+ messages in thread
From: bangerth at dealii dot org @ 2003-06-27 14:11 UTC (permalink / raw)
  To: gcc-bugs

PLEASE REPLY TO gcc-bugzilla@gcc.gnu.org ONLY, *NOT* gcc-bugs@gcc.gnu.org.

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


bangerth at dealii dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


------- Additional Comments From bangerth at dealii dot org  2003-06-27 14:11 -------
The warning is valid, and with present mainline you even get an error:

tmp/gg> c++ -c x.cc
x.cc: In function `void func()':
x.cc:15: error: ISO C++ says that these are ambiguous, even though the worst
   conversion for the first is better than the worst conversion for the second:
x.cc:8: note: candidate 1: char& Foo::operator[](unsigned int)
x.cc:15: note: candidate 2: operator[](const char*, int) <built-in>

The problem is that the argument to op[] you give is 0, which is a signed
integer. So the compiler will have to perform a conversion to unsigned 
integer to call Foo::op[]. On the other hand, it could also do the
conversion to char* using the conversion operator in Foo, and then use
the built-in conversion operator for array access.

The simplest way for you to avoid the warning is to write s[0U].

We should really have a FAQ for this...

W.


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

end of thread, other threads:[~2003-06-27 14:11 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-27  1:06 [Bug c++/11342] New: Method selection warning prevents compilation dan at photon dot com
2003-06-27  1:19 ` [Bug c++/11342] " dan at photon dot com
2003-06-27  1:36 ` dan at photon dot com
2003-06-27 14:11 ` bangerth at dealii 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).