 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
huangshan Guest
|
Posted: Thu Oct 12, 2006 7:12 am Post subject: C++ Primer, Fourth Edition |
|
|
hi all
I read "C++ Primer, Fourth Edition" ,
but can't understand this sentence
"By making the parameters const references, we allow types that do not allow
copying."
in "16.1. Template Definitions" .
who can tell me?
thanks |
|
| Back to top |
|
 |
Daniel T. Guest
|
Posted: Thu Oct 12, 2006 7:22 am Post subject: Re: C++ Primer, Fourth Edition |
|
|
In article <egka9l$1vt$1 (AT) news (DOT) cn99.com>,
"huangshan" <huangshan_canada (AT) hotmail (DOT) com> wrote:
| Quote: | hi all
I read "C++ Primer, Fourth Edition" ,
but can't understand this sentence
"By making the parameters const references, we allow types that do not allow
copying."
in "16.1. Template Definitions" .
who can tell me?
|
class NonCopyable {
NonCopyable( const NonCopyable& );
NonCopyable& operator=( const NonCopyable& );
public:
NonCopyable();
};
void fn1( NonCopyable nc ); // this will fail to compile
void fn2( const NonCopyable& nc ); // this will compile
--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longerハhave anyone to discuss your doubts with,
nor any ability to discuss them. |
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Thu Oct 12, 2006 7:27 am Post subject: Re: C++ Primer, Fourth Edition |
|
|
huangshan wrote:
| Quote: | hi all
I read "C++ Primer, Fourth Edition" ,
but can't understand this sentence
"By making the parameters const references, we allow types that do
not allow copying."
in "16.1. Template Definitions" .
who can tell me?
|
class NoCopies {
NoCopies(NoCopies const&); // copying prohibited
public:
NoCopies(int);
};
void foo(NoCopies);
void bar(const NoCopies&);
int main()
{
NoCopies nc(42);
foo(nc); // error -- must be able to copy
bar(nc); // no error
}
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask |
|
| Back to top |
|
 |
Alf P. Steinbach Guest
|
Posted: Thu Oct 12, 2006 9:10 am Post subject: Re: C++ Primer, Fourth Edition |
|
|
* Victor Bazarov:
| Quote: | huangshan wrote:
hi all
I read "C++ Primer, Fourth Edition" ,
but can't understand this sentence
"By making the parameters const references, we allow types that do
not allow copying."
in "16.1. Template Definitions" .
who can tell me?
class NoCopies {
NoCopies(NoCopies const&); // copying prohibited
public:
NoCopies(int);
};
void foo(NoCopies);
void bar(const NoCopies&);
int main()
{
NoCopies nc(42);
foo(nc); // error -- must be able to copy
bar(nc); // no error
}
|
Just to fill out the picture, the /current/ standard doesn't allow
bar( NoCopies( 42 ) );
because in the current standard, when binding a temporary to a reference
to const, the compiler is allowed to copy the temporary any number of
times, i.e. a copy constructor must be available.
So the phrasing "allow types that do not allow copying" allows an
interpretation where one believes more is allowed than is actually
allowed, but, types that do not allow copying are allowed as far as the
current standard allows (heh).
As I understand it this will be fixed in C++0x.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail? |
|
| 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
|
|