Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Should this code compile?

Reply
Thread Tools

Should this code compile?

 
 
BigMan
Guest
Posts: n/a
 
      02-23-2005
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;
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      02-23-2005
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.
 
Reply With Quote
 
 
 
 
BigMan
Guest
Posts: n/a
 
      02-23-2005
MS Visual C++ 2003 says: error C2616: 'initializing' : cannot
implicitly convert a non-lvalue 't' to a 't &' that is not const

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      02-23-2005
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);

?
 
Reply With Quote
 
E. Robert Tisdale
Guest
Posts: n/a
 
      02-23-2005
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]'
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      02-23-2005
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?

 
Reply With Quote
 
BigMan
Guest
Posts: n/a
 
      02-23-2005
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!

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      02-23-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
gems should *not be case sensitive.. or should they? botp Ruby 6 10-04-2010 11:42 PM
What the FAQs should and should not contain Josef 'Jupp' SCHUGT Ruby 0 08-19-2005 01:46 PM
taking 70-290 should i be scared? What should i expect??? Raymond Munyan MCSE 31 12-01-2004 02:34 PM
How should control images should be handled? ~~~ .NET Ed ~~~ ASP .Net Building Controls 1 11-03-2004 12:30 PM
Fire Code behind code AND Javascript code associated to a Button Click Event =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?= ASP .Net 4 02-11-2004 07:31 AM



Advertisments