 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Matthias Hofmann Guest
|
Posted: Sat Oct 09, 2004 6:58 pm Post subject: Why does T need to be copy constructible for std::min? |
|
|
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 ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Mon Oct 11, 2004 6:20 pm Post subject: Re: Why does T need to be copy constructible for std::min? |
|
|
Matthias Hofmann wrote:
| Quote: | 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 ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Tue Oct 12, 2004 9:57 pm Post subject: Re: Why does T need to be copy constructible for std::min? |
|
|
[email]v.Abazarov (AT) comAcast (DOT) net[/email] (Victor Bazarov) writes:
| Quote: | 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 ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Wed Oct 13, 2004 4:03 am Post subject: Re: Why does T need to be copy constructible for std::min? |
|
|
David Abrahams wrote:
| Quote: | v.Abazarov (AT) comAcast (DOT) 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 ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Wed Oct 13, 2004 7:01 pm Post subject: Re: Why does T need to be copy constructible for std::min? |
|
|
[email]v.Abazarov (AT) comAcast (DOT) net[/email] (Victor Bazarov) writes:
| Quote: | David Abrahams wrote:
[email]v.Abazarov (AT) comAcast (DOT) net[/email] (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.
| Quote: | 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 ;-)
| Quote: | 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 ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Oct 14, 2004 4:49 am Post subject: Re: Why does T need to be copy constructible for std::min? |
|
|
David Abrahams wrote:
| Quote: | v.Abazarov (AT) comAcast (DOT) 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.
| Quote: | 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.
|
If you have forwarded it, then I don't need to do anything.
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 ]
|
|
| Back to top |
|
 |
David Abrahams Guest
|
Posted: Thu Oct 14, 2004 8:25 pm Post subject: Re: Why does T need to be copy constructible for std::min? |
|
|
[email]v.Abazarov (AT) comAcast (DOT) net[/email] (Victor Bazarov) writes:
| Quote: | David Abrahams wrote:
[email]v.Abazarov (AT) comAcast (DOT) net[/email] (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 ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|