Should this code compile?

Discussion in 'C++' started by BigMan, Feb 23, 2005.

  1. BigMan

    BigMan Guest

    Should the following piece of code compile or not according the C++
    standard? Why?

    class t
    {
    public:
    t( ) { }
    t( t& a ) { }

    template< typename ttt >
    t( ttt ) { }
    };

    int main
    (
    )
    {
    t a;
    t b = 1;

    return 0;
    }
     
    BigMan, Feb 23, 2005
    #1
    1. Advertising

  2. BigMan wrote:
    > Should the following piece of code compile or not according the C++
    > standard? Why?
    >
    > class t
    > {
    > public:
    > t( ) { }
    > t( t& a ) { }
    >
    > template< typename ttt >
    > t( ttt ) { }
    > };
    >
    > int main
    > (
    > )
    > {
    > t a;
    > t b = 1;
    >
    > return 0;
    > }


    Is this a homework question? What does your book tell you? Have you
    tried compiling it on your compiler? Did it compile? If yes, why are
    you still in doubt? If it didn't, what messages did you get?

    Come on, work a little bit. Typing your homework questions does not
    qualify as work.
     
    Victor Bazarov, Feb 23, 2005
    #2
    1. Advertising

  3. BigMan

    BigMan Guest

    MS Visual C++ 2003 says: error C2616: 'initializing' : cannot
    implicitly convert a non-lvalue 't' to a 't &' that is not const
     
    BigMan, Feb 23, 2005
    #3
  4. BigMan wrote:
    > MS Visual C++ 2003 says: error C2616: 'initializing' : cannot
    > implicitly convert a non-lvalue 't' to a 't &' that is not const
    >


    So, what does it tell you? That the compiler is trying to use the
    copy constructor that you defined with the signature

    t(t&);

    and cannot bind a temporary it creates from '1' to a reference to
    non-const. If you redefine the copy c-tor to accept a reference to
    const, it would compile.

    Now, the question is, why is it trying to use the copy constructor?
    What is the difference between these two forms:

    t b = 1;
    and
    t b(1);

    ?
     
    Victor Bazarov, Feb 23, 2005
    #4
  5. Off Topic: Should this code compile?

    BigMan wrote:

    > Should the following piece of code compile or not
    > according the C++ standard? Why?


    Did you try to compile it?
    Your compiler will tell you whether it will compile or not
    and, if not, it will tell you why.
    If you are suspicious that your compiler
    does not comply with ANSI/ISO C++ standards,
    you can compile with the Comeau online compiler:

    http://www.comeaucomputing.com/tryitout/

    Compilers, especially the Comeau C++ compiler are much more reliable
    than subscribers to the comp.lang.c++ newsgroup
    when it comes to deciding
    whether any program is a valid C++ program or not.

    > cat main.cpp

    class t {
    public:
    t(void) { }
    t(t& a) { }

    template<typename ttt>
    t(ttt) { }
    };

    int main(int argc, char* argv[]) {
    t a;
    t b = 1;

    return 0;
    }

    > g++ -Wall -ansi -pedantic -o main main.cpp

    main.cpp: In function `int main(int, char**)':
    main.cpp:12: error: no matching function for call to `t::t(t)'
    main.cpp:4: note: candidates are: t::t(t&)
    main.cpp:12: error: initializing temporary \
    from result of `t::t(ttt) [with ttt = int]'
     
    E. Robert Tisdale, Feb 23, 2005
    #5
  6. BigMan

    Rolf Magnus Guest

    BigMan wrote:

    > MS Visual C++ 2003 says: error C2616: 'initializing' : cannot
    > implicitly convert a non-lvalue 't' to a 't &' that is not const


    The compiler is right about that. The line:

            t b = 1;

    will first use your template constructor to construct a temporary t from the
    int value 1. Then, the copy constructor is used to make b a copy of that
    temporary. Your copy constructor looks like:

            t( t& a ) { }

    So it takes a non-const reference to a t as parameter. But C++ forbids
    binding a temporary to a non-const reference.
    Are you sure your reference must be non-const here, i.e. does your copy
    constructor really modify the original?
     
    Rolf Magnus, Feb 23, 2005
    #6
  7. BigMan

    BigMan Guest

    Yes, the copy ctor DOES change the original - it transfers ownership of
    a resource that is too expensive to copy (e.g. a huge amount of
    memory).
    All this stuff about copy initilialization, direct initialization and
    the like seem to me rather silly... Most compilers will NOT call the
    copy ctor anyway in order to compile the above piece of code, so why
    should the standard require that the copy ctor be callable?!
    This and other such requirements should be relaxed in order to make the
    language even more powerful!
     
    BigMan, Feb 23, 2005
    #7
  8. BigMan

    Rolf Magnus Guest

    BigMan wrote:

    > Yes, the copy ctor DOES change the original - it transfers ownership of
    > a resource that is too expensive to copy (e.g. a huge amount of
    > memory).
    > All this stuff about copy initilialization, direct initialization and
    > the like seem to me rather silly... Most compilers will NOT call the
    > copy ctor anyway in order to compile the above piece of code, so why
    > should the standard require that the copy ctor be callable?!


    Because the correctness of code doesn't depend on optimizations.

    > This and other such requirements should be relaxed in order to make the
    > language even more powerful!


    And if it were, you might write code that needs the copy constructor on one
    compiler an doesn't on another, i.e. your code would be unportable.
     
    Rolf Magnus, Feb 23, 2005
    #8
    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. Nagaraj
    Replies:
    1
    Views:
    876
    Lionel B
    Mar 1, 2007
  2. fAnSKyer
    Replies:
    2
    Views:
    536
    Alf P. Steinbach
    Jun 7, 2009
  3. ~~~ .NET Ed ~~~

    How should control images should be handled?

    ~~~ .NET Ed ~~~, Oct 31, 2004, in forum: ASP .Net Building Controls
    Replies:
    1
    Views:
    242
    John Saunders
    Nov 3, 2004
  4. Josef 'Jupp' SCHUGT

    What the FAQs should and should not contain

    Josef 'Jupp' SCHUGT, Aug 19, 2005, in forum: Ruby
    Replies:
    0
    Views:
    196
    Josef 'Jupp' SCHUGT
    Aug 19, 2005
  5. botp
    Replies:
    6
    Views:
    219
    Joel VanderWerf
    Oct 5, 2010
Loading...

Share This Page