Templated class constructor question

F

ferdinand.stefanus

Hi, I have some questions regarding templated class constructor:

#include <iostream>

using namespace std;

template<typename T>
class Foo
{
public:
explicit Foo(const T& bar): _bar(bar)
{ cout << "In constructor, _bar = " << _bar << '\n'; }

template<typename U>
Foo(const Foo<U>& rhs): _bar(rhs.GetBar())
{ cout << "In converting constructor, _bar = " << _bar << '\n'; }

Foo(const Foo<T>& rhs): _bar(rhs._bar)
{ cout << "In copy constructor, _bar = " << _bar << '\n'; }

T GetBar() const { return _bar; }

private:
T _bar;
};

int main()
{
Foo<int> intFoo1(1), intFoo2(2);
Foo<int> intFoo3(intFoo1);
Foo<double> dblFoo1(intFoo2);
}

A quick test with VC++6 yields the following result:

In constructor, _bar = 1
In constructor, _bar = 2
In copy constructor, _bar = 1
In converting constructor, _bar = 2

Is this behaviour conforming with the standard? It seems that even if I
remove the copy constructor declaration/definition, the converting
constructor will not be used (the compiler-generated copy constructor
will be used instead). If that's the case, can I safely assume that the
converting constructor will never be called for templated class of the
same type?

Thanks!
 
V

Victor Bazarov

Hi, I have some questions regarding templated class constructor:

#include <iostream>

using namespace std;

template<typename T>
class Foo
{
public:
explicit Foo(const T& bar): _bar(bar)
{ cout << "In constructor, _bar = " << _bar << '\n'; }

template<typename U>
Foo(const Foo<U>& rhs): _bar(rhs.GetBar())
{ cout << "In converting constructor, _bar = " << _bar << '\n'; }

Foo(const Foo<T>& rhs): _bar(rhs._bar)
{ cout << "In copy constructor, _bar = " << _bar << '\n'; }

T GetBar() const { return _bar; }

private:
T _bar;
};

int main()
{
Foo<int> intFoo1(1), intFoo2(2);
Foo<int> intFoo3(intFoo1);
Foo<double> dblFoo1(intFoo2);
}

A quick test with VC++6 yields the following result:

In constructor, _bar = 1
In constructor, _bar = 2
In copy constructor, _bar = 1
In converting constructor, _bar = 2

Is this behaviour conforming with the standard?

Yes, AFAICS.
It seems that even if I
remove the copy constructor declaration/definition, the converting
constructor will not be used (the compiler-generated copy constructor
will be used instead).

Yes, that's correct.
If that's the case, can I safely assume that the
converting constructor will never be called for templated class of the
same type?

Yes, that's the Standard requirement, IIRC. I couldn't find the passage
from the Standard within 5 minutes, though...

V
 
J

Jonathan Mcdougall

Hi, I have some questions regarding templated class constructor:

#include <iostream>

using namespace std;

template<typename T>
class Foo
{
public:
explicit Foo(const T& bar): _bar(bar)
{ cout << "In constructor, _bar = " << _bar << '\n'; }

template<typename U>
Foo(const Foo<U>& rhs): _bar(rhs.GetBar())
{ cout << "In converting constructor, _bar = " << _bar << '\n'; }

Foo(const Foo<T>& rhs): _bar(rhs._bar)
{ cout << "In copy constructor, _bar = " << _bar << '\n'; }

T GetBar() const { return _bar; }

private:
T _bar;
};

int main()
{
Foo<int> intFoo1(1), intFoo2(2);
Foo<int> intFoo3(intFoo1);
Foo<double> dblFoo1(intFoo2);
}

A quick test with VC++6 yields the following result:

In constructor, _bar = 1
In constructor, _bar = 2
In copy constructor, _bar = 1
In converting constructor, _bar = 2

Is this behaviour conforming with the standard?
Yes.

It seems that even if I
remove the copy constructor declaration/definition, the converting
constructor will not be used (the compiler-generated copy constructor
will be used instead).

Templated copy-ctor, ctor and assignment operator never replace
non-templated ones, so even if you declare them, the compiler will still
generate default ones.
If that's the case, can I safely assume that the
converting constructor will never be called for templated class of the
same type?

Yes.


Jonathan
 

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

Forum statistics

Threads
474,189
Messages
2,571,016
Members
47,618
Latest member
Leemorton01

Latest Threads

Top