Should this code compile?

B

BigMan

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;
}
 
V

Victor Bazarov

BigMan said:
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.
 
B

BigMan

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

Victor Bazarov

BigMan said:
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);

?
 
E

E. Robert Tisdale

BigMan said:
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]'
 
R

Rolf Magnus

BigMan said:
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?
 
B

BigMan

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!
 
R

Rolf Magnus

BigMan said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top