C++ Primer, Fourth Edition

Discussion in 'C++' started by huangshan, Oct 12, 2006.

  1. huangshan

    huangshan Guest

    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
    huangshan, Oct 12, 2006
    #1
    1. Advertising

  2. huangshan

    Daniel T. Guest

    In article <egka9l$1vt$99.com>,
    "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 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.
    Daniel T., Oct 12, 2006
    #2
    1. Advertising

  3. 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
    }

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Oct 12, 2006
    #3
  4. * Victor Bazarov:
    > 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?
    Alf P. Steinbach, Oct 12, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Vinodh Kumar P

    Fourth edition of CPPPL

    Vinodh Kumar P, Jan 22, 2004, in forum: C++
    Replies:
    1
    Views:
    346
    E. Robert Tisdale
    Jan 22, 2004
  2. Andrew Koenig

    Announcement: C++ Primer, 4th edition

    Andrew Koenig, Jan 17, 2005, in forum: C++
    Replies:
    9
    Views:
    437
    red floyd
    Jan 19, 2005
  3. Replies:
    1
    Views:
    366
    The Ghost In The Machine
    Feb 5, 2007
  4. hajime

    C++ Primer, 4th Edition

    hajime, Aug 16, 2005, in forum: C++
    Replies:
    5
    Views:
    457
    hajime
    Aug 16, 2005
  5. asdf
    Replies:
    6
    Views:
    287
    Default User
    Sep 18, 2006
Loading...

Share This Page