473,406 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Templated class constructor question

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!

Jul 22 '05 #1
2 1897
<fe****************@gmail.com> wrote...
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
Jul 22 '05 #2
fe****************@gmail.com wrote:
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
Jul 22 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
5
by: kalita | last post by:
template<class T> class A { template<class Y> A(const A<Y> &) { // whatever } };
6
by: Dan Huantes | last post by:
I was presented a problem today where a class had member variable that was an object of a templated class. The class wanted to instantiate the object as a private member variable and call a...
9
by: shaun | last post by:
Dear all, I realized an error in a previous post, I reproduce it here because I'm still not sure how to solve it: I want to make a templated function which points to one-past-the-end of a...
7
by: Claudius | last post by:
Hello, in my class TopTen I need to define three constructors while only the last one, the most general in terms of templates, should be sufficient in my opinion: template <typename Tnum,...
3
by: Lawrence Spector | last post by:
How does one call a templated constructor inside of a class when instantiating an object? I made up a quick sample to demonstrate. #include <iostream> class TestClass { public: template...
6
by: alan | last post by:
I'm creating a sort-of "wrapper" class which (partly) acts like a variable. Something like: template<class t> class cell{ t curval; public: /*public for debugging only - will be private in...
1
by: eihabisaac | last post by:
hey everybody im having some trouble with my assignment we should write a program that will handle with a templated linkedlist for a bank account to add/delete amount to the account or sort...
2
card
by: card | last post by:
Hi everyone, I have a question about referencing a nested class contained within a templated class. Of course the best way to show you is by example. Here's my templated classes: #include...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.