473,386 Members | 1,775 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,386 software developers and data experts.

Implicit conversion constructor with template classes

Hi,

If I have:

template<class T>
class X
{
.....
};

then:

template<class U>
class Y
{
public:
typedef X<U> NormalType;
typedef X<const U> ConstType;

NormalType foo() { return NormalType(); }
};

How can I (or can I?) have conversion from NormalType to ConstType so
the following would work:

Y<int> bar;
ConstType i = bar.foo();

Can I have a constructor in Y like "Y(const Y<const T>&)" to provide
implicit conversion? If I do something in Y like:

template<class Z>
Y(const Z&)

That would apply to any type and try to convert, wouldnt it?

Is there a way I can specify I can accept a type "const T" when the
template is instantiated with just T?
Thanks for any tips.

Jun 25 '06 #1
1 3431
flopbucket wrote:
Hi,

If I have:

template<class T>
class X
{
....
};

then:

template<class U>
class Y
{
public:
typedef X<U> NormalType;
typedef X<const U> ConstType;

NormalType foo() { return NormalType(); }
};

How can I (or can I?) have conversion from NormalType to ConstType so
the following would work:

Y<int> bar;
ConstType i = bar.foo();
Presumably you can modify Y's code. Can you modify X's too?
Can I have a constructor in Y like "Y(const Y<const T>&)" to provide
implicit conversion? If I do something in Y like:

template<class Z>
Y(const Z&)

That would apply to any type and try to convert, wouldnt it?
Yes, but it would convert it to a Y, not an X.
Is there a way I can specify I can accept a type "const T" when the
template is instantiated with just T?


You have to be careful, since the type obviously needs to work with
const. In your case, something like this should work:

template <class T>
class X
{
public:
X(X<const T> const&); //construct from const.
};

template <class T>
class X<const T>
{
//no const T constructor
};

You could move common functionality into a base class to avoid excessive
repetition.

Tom
Jun 26 '06 #2

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

Similar topics

2
by: Russell Reagan | last post by:
In a newer version of a chess program I am writing, I have created classes that are (more or less) drop in replacements for things that used to be plain old integer or enumerated variables (colors,...
1
by: Christophe Poucet | last post by:
Hellom I have an issue with implicit conversions. Apparently when one calls an operator on a class Y which has a conversion operator to class X which has the operator . Sadly it will not do...
3
by: Siemel Naran | last post by:
Here is a question about implicit conversion from T (&) to ptrcarray<T>. I wrote a class template <class T> struct ptrcarray { T * array; size_t size;
5
by: Vijai Kalyan | last post by:
Hello, I have come back to C++ after a couple of years with Java so I am quite rusty and this question may seem poor: My platform is Windows XP with MSVC 7.1. I have a class with a...
7
by: Michael Lehn | last post by:
Hi, I have a question regarding the conversion of objects. When is the conversion done by the constructor and when by the operator. My feeling tells me that the constructor is preferred. But...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
3
by: Jess | last post by:
Hello, I can perform implicit conversion through constructor, like class A{ public: A(int x):a(x){}; int a; };
0
by: alexroat | last post by:
Hi, I cant understand why this code does not work. I've implemented two classes A,B that provide an array like structure (they are identical). I can convert B in A using implicit conversion, but...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.