public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Postincrement causes match problem - bug or feature?
@ 2008-09-29 15:19 Andrew Bell
  2008-09-29 15:52 ` John Love-Jensen
  0 siblings, 1 reply; 5+ messages in thread
From: Andrew Bell @ 2008-09-29 15:19 UTC (permalink / raw)
  To: gcc-help

Hi,

The following code gives a function match error.  If the postincrement
is removed on cp when calling foo(), everything is fine.  Is this a
compiler problem or a user problem?

Thanks:

=================

#include <iostream>

using namespace std;

template <typename T>
int foo(T& t1)
{
  cerr << "Got in #1!\n";
  return 0;
}

int foo(char* & c)
{
  cerr << "Got in #2!\n";
  return 0;
}

int main()
{
  char c = 'c';
  char* cp = &c;

  foo(cp++);
  return (0);
}

==============

test2.cc: In function 'int main()':
test2.cc:23: error: no matching function for call to 'foo(char*)'
test2.cc:6: note: candidates are: int foo(T&) [with T = char*]
test2.cc:12: note:                 int foo(char*&)

==============

$ gcc -v
Using built-in specs.
Target: x86_64-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man
--infodir=/usr/share/info --enable-shared --enable-threads=posix
--enable-checking=release --with-system-zlib --enable-__cxa_atexit
--disable-libunwind-exceptions --enable-libgcj-multifile
--enable-languages=c,c++,objc,obj-c++,java,fortran,ada
--enable-java-awt=gtk --disable-dssi --enable-plugin
--with-java-home=/usr/lib/jvm/java-1.4.2-gcj-1.4.2.0/jre
--with-cpu=generic --host=x86_64-redhat-linux
Thread model: posix
gcc version 4.1.2 20070626 (Red Hat 4.1.2-14)

-- 
Andrew Bell
andrew.bell.ia@gmail.com

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

* Re: Postincrement causes match problem - bug or feature?
  2008-09-29 15:19 Postincrement causes match problem - bug or feature? Andrew Bell
@ 2008-09-29 15:52 ` John Love-Jensen
  2008-09-29 16:03   ` Andrew Bell
  0 siblings, 1 reply; 5+ messages in thread
From: John Love-Jensen @ 2008-09-29 15:52 UTC (permalink / raw)
  To: Andrew Bell, GCC-help

Hi Andrew,

The compiler cannot decide which of the two foo functions are more suitable,
since they are both equally suitable.

I presume you wanted the "Got in #1" routine to be called, given that you
are passing in a char* rvalue.  (If you were passing in an lvalue, then the
"Got in #2" would be the match.)

HTH,
--Eljay

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

* Re: Postincrement causes match problem - bug or feature?
  2008-09-29 15:52 ` John Love-Jensen
@ 2008-09-29 16:03   ` Andrew Bell
  2008-09-29 16:50     ` John Fine
  2008-09-29 16:54     ` John Fine
  0 siblings, 2 replies; 5+ messages in thread
From: Andrew Bell @ 2008-09-29 16:03 UTC (permalink / raw)
  To: John Love-Jensen; +Cc: GCC-help

On Mon, Sep 29, 2008 at 10:51 AM, John Love-Jensen <eljay@adobe.com> wrote:
> Hi Andrew,
>
> The compiler cannot decide which of the two foo functions are more suitable,
> since they are both equally suitable.

The real question is why the postincrement makes a difference.  Can you explain?

-- 
Andrew Bell
andrew.bell.ia@gmail.com

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

* Re: Postincrement causes match problem - bug or feature?
  2008-09-29 16:03   ` Andrew Bell
@ 2008-09-29 16:50     ` John Fine
  2008-09-29 16:54     ` John Fine
  1 sibling, 0 replies; 5+ messages in thread
From: John Fine @ 2008-09-29 16:50 UTC (permalink / raw)
  To: Andrew Bell; +Cc: John Love-Jensen, GCC-help

The postincrement makes a difference because the result of the 
postincrement is a temporary value.

You can't pass a temporary value by non const reference.

IIUC, your results would be different if the first definition had const 
in it as follows:

template <typename T>
int foo(T const& t1)
{
  cerr << "Got in #1!\n";
  return 0;
}



Andrew Bell wrote:
> On Mon, Sep 29, 2008 at 10:51 AM, John Love-Jensen <eljay@adobe.com> wrote:
>   
>> Hi Andrew,
>>
>> The compiler cannot decide which of the two foo functions are more suitable,
>> since they are both equally suitable.
>>     
>
> The real question is why the postincrement makes a difference.  Can you explain?
>
>   

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

* Re: Postincrement causes match problem - bug or feature?
  2008-09-29 16:03   ` Andrew Bell
  2008-09-29 16:50     ` John Fine
@ 2008-09-29 16:54     ` John Fine
  1 sibling, 0 replies; 5+ messages in thread
From: John Fine @ 2008-09-29 16:54 UTC (permalink / raw)
  To: Andrew Bell; +Cc: John Love-Jensen, GCC-help

The postincrement makes a difference because the result of the 
postincrement is a temporary value.

You can't pass a temporary value by non const reference.

IIUC, your results would be different if the first definition had const 
in it as follows:

template <typename T>
int foo(T const& t1)
{
  cerr << "Got in #1!\n";
  return 0;
}



Andrew Bell wrote:
> On Mon, Sep 29, 2008 at 10:51 AM, John Love-Jensen <eljay@adobe.com> wrote:
>   
>> Hi Andrew,
>>
>> The compiler cannot decide which of the two foo functions are more suitable,
>> since they are both equally suitable.
>>     
>
> The real question is why the postincrement makes a difference.  Can you explain?
>
>   

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

end of thread, other threads:[~2008-09-29 16:54 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-09-29 15:19 Postincrement causes match problem - bug or feature? Andrew Bell
2008-09-29 15:52 ` John Love-Jensen
2008-09-29 16:03   ` Andrew Bell
2008-09-29 16:50     ` John Fine
2008-09-29 16:54     ` John Fine

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).