Topic: Why does T need to be copy constructible for std::min?


Author: hofmann@anvil-soft.com ("Matthias Hofmann")
Date: Sat, 9 Oct 2004 18:58:02 GMT
Raw View
Hello!

I got a question which I have recently posted on comp.lang.c++.moderated
without getting an answer, so I'm trying to find one here...

Section 25.3.7 is about the requirements of T in std::min (and std::max),
which is that T be LessThanComparable and CopyConstructible. As far as I
know, std::min is defined and usually implemented like so:

template <class T> inline const T& min( const T& a, const T& b )
{
     return a < b ? a : b;
}

As everyone can see, the objects of type T are passed by reference, and a
reference is returned as well. So I wonder: where does the compiler need to
make a copy of a or b, so that the requirement of T being CopyConstructible
is justified?

Best regards,

Matthias Hofmann



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@comAcast.net (Victor Bazarov)
Date: Mon, 11 Oct 2004 18:20:32 GMT
Raw View
Matthias Hofmann wrote:
> I got a question which I have recently posted on comp.lang.c++.moderated
> without getting an answer, so I'm trying to find one here...
>
> Section 25.3.7 is about the requirements of T in std::min (and std::max),
> which is that T be LessThanComparable and CopyConstructible. As far as I
> know, std::min is defined and usually implemented like so:
>
> template <class T> inline const T& min( const T& a, const T& b )
> {
>      return a < b ? a : b;
> }
>
> As everyone can see, the objects of type T are passed by reference, and a
> reference is returned as well. So I wonder: where does the compiler need to
> make a copy of a or b, so that the requirement of T being CopyConstructible
> is justified?

I think the reason is in the possibility to use a temporary object as
either of the arguments to 'min'.  The Standard says that when binding
a reference to a temporary is happening, another temporary may be
necessary.  Even if the compiler can avoid creating the other temporary,
the ability has to exist.  See 12.2 and related clauses.

Victor

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dave@boost-consulting.com (David Abrahams)
Date: Tue, 12 Oct 2004 21:57:04 GMT
Raw View
v.Abazarov@comAcast.net (Victor Bazarov) writes:

> Matthias Hofmann wrote:
>> I got a question which I have recently posted on comp.lang.c++.moderated
>> without getting an answer, so I'm trying to find one here...
>> Section 25.3.7 is about the requirements of T in std::min (and
>> std::max),
>> which is that T be LessThanComparable and CopyConstructible. As far as I
>> know, std::min is defined and usually implemented like so:
>> template <class T> inline const T& min( const T& a, const T& b )
>> {
>>      return a < b ? a : b;
>> }
>> As everyone can see, the objects of type T are passed by reference,
>> and a
>> reference is returned as well. So I wonder: where does the compiler need to
>> make a copy of a or b, so that the requirement of T being CopyConstructible
>> is justified?
>
> I think the reason is in the possibility to use a temporary object as
> either of the arguments to 'min'.  The Standard says that when binding
> a reference to a temporary is happening, another temporary may be
> necessary.  Even if the compiler can avoid creating the other temporary,
> the ability has to exist.  See 12.2 and related clauses.

I thought about that, but it doesn't seem like a good enough reason to
require a copy ctor.  After all, the argument could be an lvalue.  I
think this is a defect or at least an overly restrictive
specification.

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@comAcast.net (Victor Bazarov)
Date: Wed, 13 Oct 2004 04:03:30 GMT
Raw View
David Abrahams wrote:
> v.Abazarov@comAcast.net (Victor Bazarov) writes:
>
>
>>Matthias Hofmann wrote:
>>
>>>I got a question which I have recently posted on comp.lang.c++.moderated
>>>without getting an answer, so I'm trying to find one here...
>>>Section 25.3.7 is about the requirements of T in std::min (and
>>>std::max),
>>>which is that T be LessThanComparable and CopyConstructible. As far as I
>>>know, std::min is defined and usually implemented like so:
>>>template <class T> inline const T& min( const T& a, const T& b )
>>>{
>>>     return a < b ? a : b;
>>>}
>>>As everyone can see, the objects of type T are passed by reference,
>>>and a
>>>reference is returned as well. So I wonder: where does the compiler need to
>>>make a copy of a or b, so that the requirement of T being CopyConstructible
>>>is justified?
>>
>>I think the reason is in the possibility to use a temporary object as
>>either of the arguments to 'min'.  The Standard says that when binding
>>a reference to a temporary is happening, another temporary may be
>>necessary.  Even if the compiler can avoid creating the other temporary,
>>the ability has to exist.  See 12.2 and related clauses.
>
>
> I thought about that, but it doesn't seem like a good enough reason to
> require a copy ctor.  After all, the argument could be an lvalue.  I
> think this is a defect or at least an overly restrictive
> specification.

Then you probably should request clarification on a particular clause
of the Standard or the rationale behind a particular decision.  So far,
the Standard is what it is, and whatever I or you think doesn't really
matter.

I'd start another thread with the request (for clarification).

V

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dave@boost-consulting.com (David Abrahams)
Date: Wed, 13 Oct 2004 19:01:01 GMT
Raw View
v.Abazarov@comAcast.net (Victor Bazarov) writes:

> David Abrahams wrote:
>> v.Abazarov@comAcast.net (Victor Bazarov) writes:
>>
>>>Matthias Hofmann wrote:
>>>
>>>>I got a question which I have recently posted on comp.lang.c++.moderated
>>>>without getting an answer, so I'm trying to find one here...
>>>>Section 25.3.7 is about the requirements of T in std::min (and
>>>>std::max),
>>>>which is that T be LessThanComparable and CopyConstructible. As far as I
>>>>know, std::min is defined and usually implemented like so:
>>>>template <class T> inline const T& min( const T& a, const T& b )
>>>>{
>>>>     return a < b ? a : b;
>>>>}
>>>>As everyone can see, the objects of type T are passed by reference,
>>>>and a
>>>>reference is returned as well. So I wonder: where does the compiler need to
>>>>make a copy of a or b, so that the requirement of T being CopyConstructible
>>>>is justified?
>>>
>>>I think the reason is in the possibility to use a temporary object as
>>>either of the arguments to 'min'.  The Standard says that when binding
>>>a reference to a temporary is happening, another temporary may be
>>>necessary.  Even if the compiler can avoid creating the other temporary,
>>>the ability has to exist.  See 12.2 and related clauses.
>> I thought about that, but it doesn't seem like a good enough reason
>> to
>> require a copy ctor.  After all, the argument could be an lvalue.  I
>> think this is a defect or at least an overly restrictive
>> specification.
>
> Then you probably should request clarification on a particular clause
> of the Standard or the rationale behind a particular decision.

Why me?  You can do it if you want something clarified.

> So far, the Standard is what it is, and whatever I or you think
> doesn't really matter.

I think I have a pretty good idea how the process works, thanks ;-)

> I'd start another thread with the request (for clarification).

Then please do.  I have already forwarded the thread to the LWG chair
so we could get it on the issues list.

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: dave@boost-consulting.com (David Abrahams)
Date: Thu, 14 Oct 2004 20:25:49 GMT
Raw View
v.Abazarov@comAcast.net (Victor Bazarov) writes:

> David Abrahams wrote:
>> v.Abazarov@comAcast.net (Victor Bazarov) writes:
>>
>>>David Abrahams wrote:
>>>
>>>>I thought about that, but it doesn't seem like a good enough reason
>>>>to
>>>>require a copy ctor.  After all, the argument could be an lvalue.  I
>>>>think this is a defect or at least an overly restrictive
>>>>specification.
>>>
>>>Then you probably should request clarification on a particular clause
>>> of the Standard or the rationale behind a particular decision.
>> Why me?  You can do it if you want something clarified.
>
> Wasn't it you who said "I think this is a defect or at least an overly
> restrictive specification".  I don't think it's a defect, nor do I need
> anything clarified.  You seemed to need clarification, that's why I
> suggested that you should request it.

I didn't, but thanks.  I have formed an opinion and I don't need
anyone to clarify anything.

--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]