Topic: initialization of function parameter which is an object - not


Author: "subramanian100in@yahoo.com, India" <subramanian100in@yahoo.com>
Date: Sun, 25 Nov 2007 18:12:44 CST
Raw View
Suppose we have a class named Test.

Test obj; // assuming default ctor is available
Test direct_init(obj); // direct initialization
Test copy_init = obj; // copy initialization

Suppose we have a function

void fn(Test arg);

When we call fn() with fn(obj), the copy ctor
is called to construct 'arg'. Am I correct?
Question is what kind of initialization - direct
initialization or copy initialization, happens to
construct arg ?
What does the standard say in this regard ?
If copy initialization happens, then if the copy
ctor is 'explicit', this will not compile. Am I correct ?

The reason for asking this question is the following:
Consider the program:
(For learning purpose I have kept the copy ctor
'explicit')

#include <iostream>

using namespace std;

class Test
{
public:
Test(int arg = 0) : val(arg) { }

explicit Test(Test &arg)
{
val = arg.val;
cout << "Test(Test &arg)" << endl;
}

explicit Test(const Test &arg)
{
val = arg.val;
cout << "Test(const Test &arg)" << endl;
}

private:
int val;
};

void fn(Test t)
{
        return;
}

int main()
{
        Test obj;
        fn(obj);
        return 0;
}

This program gives compilation error under g++ with
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp
for the line
fn(obj);

The actual error message is
In function `int main()':
error: no matching function for call to `Test::Test(Test&)'
note: candidates are: Test::Test(int)
error:   initializing argument 1 of `void fn(Test)'

However this program compiles fine with VC++ 2005 Express Edition.
It produces the output
Test(Test &arg)

Why doesn't the program compile with g++ ?
What is the expected behaviour ?

Kindly explain.

Thanks
V.Subramanian

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: andreytarasevich@hotmail.com (Andrey Tarasevich)
Date: Mon, 26 Nov 2007 19:10:12 GMT
Raw View
subramanian100in@yahoo.com, India wrote:
> Suppose we have a class named Test.
>
> Test obj; // assuming default ctor is available
> Test direct_init(obj); // direct initialization
> Test copy_init = obj; // copy initialization
>
> Suppose we have a function
>
> void fn(Test arg);
>
> When we call fn() with fn(obj), the copy ctor
> is called to construct 'arg'. Am I correct?

Yes.

> Question is what kind of initialization - direct
> initialization or copy initialization, happens to
> construct arg ?
> What does the standard say in this regard ?

Copy-initialization (see 8.5/12).

However, one important thing about it is that in this case you
initialize an object of type 'Test' with an initializer of the same type
'Test', i.e. you have the same type on both sides of initialization. In
cases like that, copy-initialization is actually performed in virtually
the same way as direct-initialization (see 8.5/14). The constructor is
selected and called to directly initialize the object (as opposed to
creation of temporary object followed by copy-constructor call in case
of "true" copy-initialization).

In other words, copy-initialization with identical types on left- and
right-hand side (ignoring cv-qualifications) is semantically equivalent
to direct-initialization, with one exception: 'explicit' constructors
are not considered, since they shall only used (see 12.3.1/2) when
either the direct-initialization syntax or the cast syntax is explicitly
present.

To put it in a more organized fashion, one can think of it as if there
are not two forms of initialization in c++ (direct- and copy-), but
three of them:

1. Direct-initialization - ... (you know what it is)

2. Copy-initialization for identical types - looks like
copy-initialization, but works like direct-initialization, except that
'explicit' constructors are not allowed

3. Copy-initialization for different types - ... ("true"
copy-initialization).

In your examples you use the form number 2.

> If copy initialization happens, then if the copy
> ctor is 'explicit', this will not compile. Am I correct ?

Yes, you are.

--
Best regards,
Andrey Tarasevich

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: alfps@start.no ("Alf P. Steinbach")
Date: Mon, 26 Nov 2007 19:48:32 GMT
Raw View
* subramanian100in@yahoo.com, India:
> Suppose we have a class named Test.
>
> Test obj; // assuming default ctor is available
> Test direct_init(obj); // direct initialization
> Test copy_init = obj; // copy initialization
>
> Suppose we have a function
>
> void fn(Test arg);
>
> When we call fn() with fn(obj), the copy ctor
> is called to construct 'arg'. Am I correct?
> Question is what kind of initialization - direct
> initialization or copy initialization, happens to
> construct arg ?
> What does the standard say in this regard ?
> If copy initialization happens, then if the copy
> ctor is 'explicit', this will not compile. Am I correct ?

Hm, what was wrong with the answers you got in [comp.lang.c++], to
literally the same question?

<url:
http://groups.google.com/group/comp.lang.c++/browse_thread/thread/1fd0ed1fd619b950/eeb20def369921d0>

Looking forward to a clarification of that; it would be nice if you
could post that also in the original [comp.lang.c++] thread.

Cheers,

- Alf

--
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?

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]





Author: "subramanian100in@yahoo.com, India" <subramanian100in@yahoo.com>
Date: Tue, 27 Nov 2007 14:09:52 CST
Raw View
On Nov 27, 12:48 am, al...@start.no ("Alf P. Steinbach") wrote:
>
> Hm, what was wrong with the answers you got in [comp.lang.c++], to
> literally the same question?
>
> <url:http://groups.google.com/group/comp.lang.c++/browse_thread/thread/1fd...>
>
> Looking forward to a clarification of that; it would be nice if you
> could post that also in the original [comp.lang.c++] thread.
>
> Cheers,
>
> - Alf

Hello Alf P. Steinbach,
I got clarified with your answer in comp.lang.c++ for the same
question.
I thank you for that.
But I had sent this question to comp.std.c++ three days ago. Since I
didn't get the question posted in comp.std.c++, I thought it got
rejected by the moderator in comp.std.c++. So I posted the same
question in comp.lang.c++.

I regret if I caused any inconvenience to you.

Hope this clarifies.

Thanks
V.Subramanian

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]