C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Templated class constructor question

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
ferdinand.stefanus@gmail.
Guest





PostPosted: Tue Dec 21, 2004 2:42 am    Post subject: Templated class constructor question Reply with quote



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 Foo(const Foo<U>& rhs): _bar(rhs.GetBar())
{ cout << "In converting constructor, _bar = " << _bar << 'n'; }

Foo(const Foo { cout << "In copy constructor, _bar = " << _bar << 'n'; }

T GetBar() const { return _bar; }

private:
T _bar;
};

int main()
{
Foo 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!

Back to top
Victor Bazarov
Guest





PostPosted: Tue Dec 21, 2004 4:04 am    Post subject: Re: Templated class constructor question Reply with quote



<ferdinand.stefanus (AT) gmail (DOT) com> wrote...
Quote:
Hi, I have some questions regarding templated class constructor:

#include <iostream

using namespace std;

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

template Foo(const Foo { cout << "In converting constructor, _bar = " << _bar << 'n'; }

Foo(const Foo { cout << "In copy constructor, _bar = " << _bar << 'n'; }

T GetBar() const { return _bar; }

private:
T _bar;
};

int main()
{
Foo 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.

Quote:
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.

Quote:
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



Back to top
Jonathan Mcdougall
Guest





PostPosted: Tue Dec 21, 2004 7:40 am    Post subject: Re: Templated class constructor question Reply with quote



[email]ferdinand.stefanus (AT) gmail (DOT) com[/email] wrote:
Quote:
Hi, I have some questions regarding templated class constructor:

#include <iostream

using namespace std;

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

template Foo(const Foo { cout << "In converting constructor, _bar = " << _bar << 'n'; }

Foo(const Foo { cout << "In copy constructor, _bar = " << _bar << 'n'; }

T GetBar() const { return _bar; }

private:
T _bar;
};

int main()
{
Foo 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.

Quote:
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.

Quote:
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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.