public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Why can not use reference in operator function?
@ 2013-11-23 17:03 Parmenides
  2013-11-24 18:19 ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Parmenides @ 2013-11-23 17:03 UTC (permalink / raw)
  To: gcc-help

Hi,

I have the following code:

#include <iostream>
#include <cstring>
using namespace std;

class Int{
int x;

public:
Int(int i = 0)
{
x = i;
}

~Int()
{
}

void show()
{
cout << x << endl;
}

friend Int operator+(Int &a, Int &b)  // Because 'b' is a reference to
object rather than an object
{
return Int(a.x+b.x);
}
};

int main()
{
Int i(3), j;
j = i + Int(6);   // This can not call constructor.
j.show();

return 0;
}

Gcc issues error message, but VC++ 2010 compile it successfully. I
tried to modify

                friend Int operator+(Int &a, Int &b)

to

                friend Int operator+(Int a, Int b)

both compiler can get it pass. Why Gcc does not want to convert a
'int' to a 'Int' object in the  '+' operator function when its second
parameter is a reference to object rather than an objetc?

I further modified

                j = i + Int(6);

to

               j = i + 6;

both compiler can get it pass again. Therefore, I think it seem that
Gcc encourage programmers to use implict conversion like 'i + 6'
rather that explict conversion like 'i + Int(6)'. Is this right? If
so, does this practice comfore to the C++ standard?

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

* Re: Why can not use reference in operator function?
  2013-11-23 17:03 Why can not use reference in operator function? Parmenides
@ 2013-11-24 18:19 ` Jonathan Wakely
       [not found]   ` <CAOXENUgy1FadKfQd6uv88qoYPUMR3z9sx083up3cWkbCNTFwcg@mail.gmail.com>
  0 siblings, 1 reply; 4+ messages in thread
From: Jonathan Wakely @ 2013-11-24 18:19 UTC (permalink / raw)
  To: Parmenides; +Cc: gcc-help

On 23 November 2013 16:54, Parmenides <mobile.parmenides@gmail.com> wrote:
> Hi,
>
> I have the following code:
>
> #include <iostream>
> #include <cstring>
> using namespace std;
>
> class Int{
> int x;
>
> public:
> Int(int i = 0)
> {
> x = i;
> }
>
> ~Int()
> {
> }
>
> void show()
> {
> cout << x << endl;
> }
>
> friend Int operator+(Int &a, Int &b)  // Because 'b' is a reference to
> object rather than an object
> {
> return Int(a.x+b.x);
> }
> };
>
> int main()
> {
> Int i(3), j;
> j = i + Int(6);   // This can not call constructor.

This creates a temporary object and temporaries cannot bind to
non-const references.

> j.show();
>
> return 0;
> }
>
> Gcc issues error message, but VC++ 2010 compile it successfully. I

This is a well-known VC++ bug, it allows temporaries to bind to
non-const references. That does not conform to the C++ standard.

> tried to modify
>
>                 friend Int operator+(Int &a, Int &b)
>
> to
>
>                 friend Int operator+(Int a, Int b)
>
> both compiler can get it pass. Why Gcc does not want to convert a
> 'int' to a 'Int' object in the  '+' operator function when its second
> parameter is a reference to object rather than an objetc?

Because a temporary cannot bind to a non-const reference.

> I further modified
>
>                 j = i + Int(6);
>
> to
>
>                j = i + 6;
>
> both compiler can get it pass again. Therefore, I think it seem that
> Gcc encourage programmers to use implict conversion like 'i + 6'
> rather that explict conversion like 'i + Int(6)'. Is this right?

No, GCC encourages you to use const references if you want to bind to
temporaries, as required by the C++ standard.

Your operator should have been declared

                friend Int operator+(const Int& a, const Int& b)


> If
> so, does this practice comfore to the C++ standard?

G++ conforms to the standard in this regard.

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

* Re: Why can not use reference in operator function?
       [not found]     ` <CAH6eHdTHpCNtMb0EOrs034mifZ9j-r=CwyYgkTHM2J1uN6dmVg@mail.gmail.com>
@ 2013-11-26 13:23       ` Parmenides
  2013-11-26 13:41         ` Jonathan Wakely
  0 siblings, 1 reply; 4+ messages in thread
From: Parmenides @ 2013-11-26 13:23 UTC (permalink / raw)
  To: gcc-help

Hi Wakely,

Thanks for your reply. The code has gotten compiled successfully with
both compliers. I wonder what is the difference between 'Int &' and
'const Int &'. Why the former fails,  while the latter causes the
conversion from an 'int' to an 'Int' object?

2013/11/26 Jonathan Wakely <jwakely.gcc@gmail.com>:
> Please reply to the mailing list, not me.
>
> On Nov 25, 2013 1:23 PM, "Parmenides" <mobile.parmenides@gmail.com> wrote:
>>
>> Hi Wakely,
>>
>> Thanks for your reply. The code has gotten compiled successfully with
>> both compliers. I wonder what is the difference between 'Int &' and
>> 'const Int &'. Why the former fails,  while the latter causes the
>> conversion from an 'int' to an 'Int' object?
>>
>>
>> 2013/11/24 Jonathan Wakely <jwakely.gcc@gmail.com>:
>> > On 23 November 2013 16:54, Parmenides <mobile.parmenides@gmail.com>
>> > wrote:
>> >> Hi,
>> >>
>> >> I have the following code:
>> >>
>> >> #include <iostream>
>> >> #include <cstring>
>> >> using namespace std;
>> >>
>> >> class Int{
>> >> int x;
>> >>
>> >> public:
>> >> Int(int i = 0)
>> >> {
>> >> x = i;
>> >> }
>> >>
>> >> ~Int()
>> >> {
>> >> }
>> >>
>> >> void show()
>> >> {
>> >> cout << x << endl;
>> >> }
>> >>
>> >> friend Int operator+(Int &a, Int &b)  // Because 'b' is a reference to
>> >> object rather than an object
>> >> {
>> >> return Int(a.x+b.x);
>> >> }
>> >> };
>> >>
>> >> int main()
>> >> {
>> >> Int i(3), j;
>> >> j = i + Int(6);   // This can not call constructor.
>> >
>> > This creates a temporary object and temporaries cannot bind to
>> > non-const references.
>> >
>> >> j.show();
>> >>
>> >> return 0;
>> >> }
>> >>
>> >> Gcc issues error message, but VC++ 2010 compile it successfully. I
>> >
>> > This is a well-known VC++ bug, it allows temporaries to bind to
>> > non-const references. That does not conform to the C++ standard.
>> >
>> >> tried to modify
>> >>
>> >>                 friend Int operator+(Int &a, Int &b)
>> >>
>> >> to
>> >>
>> >>                 friend Int operator+(Int a, Int b)
>> >>
>> >> both compiler can get it pass. Why Gcc does not want to convert a
>> >> 'int' to a 'Int' object in the  '+' operator function when its second
>> >> parameter is a reference to object rather than an objetc?
>> >
>> > Because a temporary cannot bind to a non-const reference.
>> >
>> >> I further modified
>> >>
>> >>                 j = i + Int(6);
>> >>
>> >> to
>> >>
>> >>                j = i + 6;
>> >>
>> >> both compiler can get it pass again. Therefore, I think it seem that
>> >> Gcc encourage programmers to use implict conversion like 'i + 6'
>> >> rather that explict conversion like 'i + Int(6)'. Is this right?
>> >
>> > No, GCC encourages you to use const references if you want to bind to
>> > temporaries, as required by the C++ standard.
>> >
>> > Your operator should have been declared
>> >
>> >                 friend Int operator+(const Int& a, const Int& b)
>> >
>> >
>> >> If
>> >> so, does this practice comfore to the C++ standard?
>> >
>> > G++ conforms to the standard in this regard.

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

* Re: Why can not use reference in operator function?
  2013-11-26 13:23       ` Parmenides
@ 2013-11-26 13:41         ` Jonathan Wakely
  0 siblings, 0 replies; 4+ messages in thread
From: Jonathan Wakely @ 2013-11-26 13:41 UTC (permalink / raw)
  To: Parmenides; +Cc: gcc-help

On 26 November 2013 13:17, Parmenides wrote:
> Hi Wakely,
>
> Thanks for your reply. The code has gotten compiled successfully with
> both compliers. I wonder what is the difference between 'Int &' and
> 'const Int &'.

That is a basic C++ question, so is not suitable for this mailing
list. See http://www.parashift.com/c++-faq/ref-to-const.html

> Why the former fails,  while the latter causes the
> conversion from an 'int' to an 'Int' object?

Again, this mailing list is not the right place to try and learn C++.

The conversion from int to Int creates a temporary object, and
temporary Int objects cannot bind to Int& references, they can only
bind to const Int& reference. That's how C++ works.

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

end of thread, other threads:[~2013-11-26 13:34 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-23 17:03 Why can not use reference in operator function? Parmenides
2013-11-24 18:19 ` Jonathan Wakely
     [not found]   ` <CAOXENUgy1FadKfQd6uv88qoYPUMR3z9sx083up3cWkbCNTFwcg@mail.gmail.com>
     [not found]     ` <CAH6eHdTHpCNtMb0EOrs034mifZ9j-r=CwyYgkTHM2J1uN6dmVg@mail.gmail.com>
2013-11-26 13:23       ` Parmenides
2013-11-26 13:41         ` Jonathan Wakely

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