473,785 Members | 2,767 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Template casting operator

Hello,
Why does my cast from Vector<class Float> to Vector<float> not work? It
won't compile,

template<class Float> class Vector
{
public:
Vector(Float x1,Float y1,Float z1):x(x1),y(y1) ,z(z1){}
inline Vector<float> operator() () const;

Float x,y,z;
};

template <class Float> inline Vector<float>
Vector<Float>:: operator() () const
{
return Vector<float>(( float)x,(float) y,(float)z);
}
int main()
{
Vector<double> pd(5.6,3.4,2.4) ;

Vector<float> pf=(Vector<floa t>)pd; /* compiler error here */

}

I'd ideally like to be able to cast a Vector<double> to a Vector<float>.


Jul 22 '05 #1
21 2192
"Makhno" <ro**@127.0.0.1 > wrote...
Why does my cast from Vector<class Float> to Vector<float> not work?
Because you didn't use the operator() you wrote. But that's not what
bothers you, is it? Conversion from Vector<type1> to Vector<type2>
is something that can be easily obtained, given the right function.
See below.
It
won't compile,

template<class Float> class Vector
{
public:
Vector(Float x1,Float y1,Float z1):x(x1),y(y1) ,z(z1){}
inline Vector<float> operator() () const;
Declaring something 'inline' without providing its body is useless.

Back to your problem... You probably wanted to create a templated
type conversion operator:

template<class S> operator Vector<S>() const;

Float x,y,z;
};

template <class Float> inline Vector<float>
Vector<Float>:: operator() () const
{
return Vector<float>(( float)x,(float) y,(float)z);
}
Make this

template<class F> template<class D>
Vector<F>::oper ator Vector<D>() const
{
return Vector<D>(x, y, z);
}


int main()
{
Vector<double> pd(5.6,3.4,2.4) ;

Vector<float> pf=(Vector<floa t>)pd; /* compiler error here */
Try it now.

}

I'd ideally like to be able to cast a Vector<double> to a Vector<float>.


C-style casts are unnecessary if proper conversion is provided.

---------------------------- code that compiles -------------------------
template<class F> class Vector
{
public:
Vector(F x1, F y1, F z1):x(x1),y(y1) ,z(z1){}
template<class D> operator Vector<D> () const;
F x,y,z;
};

template <class F> template<class D>
Vector<F>::oper ator Vector<D> () const
{
return Vector<D>(x,y,z );
}

int main()
{
Vector<double> pd(5.6,3.4,2.4) ;
Vector<float> pf = pd; /* no compiler error here */
}
---------------------------------------------------------------------------

Victor
Jul 22 '05 #2

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:43gUb.1743 38$Rc4.1320258@ attbi_s54...
"Makhno" <ro**@127.0.0.1 > wrote...
Why does my cast from Vector<class Float> to Vector<float> not
work?
<snip>

Back to your problem... You probably wanted to create a templated
type conversion operator:

template<class S> operator Vector<S>() const;


Your diagnosis is correct, of course, and you solution works, but
wouldn't it be more natural to define a converting constructor in this
case?

I'd write a conversion operator if I was not able to modify the
definition of the target type (e.g. converting to int or a pointer
type) or if I by using an operator I could avoid constucting a new
object (e.g. by returning a reference to a member.) With conversions
between specializations of the same template, I'd tend to use a
converting constructor.

Jonathan
Jul 22 '05 #3
Did you notice you've defined the operator (),
not the cast operator?

"Makhno" <ro**@127.0.0.1 > wrote in message news:<bv******* ***@news6.svr.p ol.co.uk>...
Hello,
Why does my cast from Vector<class Float> to Vector<float> not work? It
won't compile,

template<class Float> class Vector
{
public:
Vector(Float x1,Float y1,Float z1):x(x1),y(y1) ,z(z1){}
inline Vector<float> operator() () const;

Float x,y,z;
};

template <class Float> inline Vector<float>
Vector<Float>:: operator() () const
{
return Vector<float>(( float)x,(float) y,(float)z);
}
int main()
{
Vector<double> pd(5.6,3.4,2.4) ;

Vector<float> pf=(Vector<floa t>)pd; /* compiler error here */

}

I'd ideally like to be able to cast a Vector<double> to a Vector<float>.

Jul 22 '05 #4
> Declaring something 'inline' without providing its body is useless.

Do you mean putting 'inline' in the declaration is useless, or are you
refering to how I've put the word 'inline' in both the declaration and
definition? (a habbit I got into when VS6 once acted strange unless I did
this)
---------------------------- code that compiles -------------------------


Afraid not. I get "unrecogniz able template declaration/definition" from
..NET, then a ton of other errors. But it is the kind of thing I'm looking
for, just didn't know how to get the syntax correct.

Jul 22 '05 #5
"Makhno" <ro**@127.0.0.1 > wrote...
Declaring something 'inline' without providing its body is useless.
Do you mean putting 'inline' in the declaration is useless, or are you
refering to how I've put the word 'inline' in both the declaration and
definition? (a habbit I got into when VS6 once acted strange unless I did
this)


'inline' is but a suggestion to the compiler. The compiler is free to
completely ignore it. Given that you put it in a declaration, what should
a compiler do when it sees a call to the function? Where would the compiler
take the body necessary to make an inline function expansion? So, adding
'inline' to a declaration is meaningless and is probably simply ignored by
the compiler.

Supplying 'inline' with the definition is perfectly fine and is usually
done when the definition is in the same header but outside the class, mind
you, without 'inline' in such case you are likely to have a multiple
definition error (if you happen to use that header in more than one
translation unit).
---------------------------- code that
compiles -------------------------
Afraid not. I get "unrecogniz able template declaration/definition" from
.NET, then a ton of other errors. But it is the kind of thing I'm looking
for, just didn't know how to get the syntax correct.


Contact VC++ people, then. It is quite possible they haven't got their
compiler ready for the real world yet. When I write "compiles", I make sure
to test it.

Good luck!

Victor
Jul 22 '05 #6

"Makhno" <ro**@127.0.0.1 > wrote in message
news:bv******** **@newsg3.svr.p ol.co.uk...
Declaring something 'inline' without providing its body is
useless.
Afraid not. I get "unrecogniz able template declaration/definition" from .NET, then a ton of other errors. But it is the kind of thing I'm looking for, just didn't know how to get the syntax correct.


Works fine on VC7.1. I think for VC7.0 you have to define the operator
in-class.

Jonathan
Jul 22 '05 #7
> Supplying 'inline' with the definition is perfectly fine and is usually
done when the definition is in the same header but outside the class, mind
you, without 'inline' in such case you are likely to have a multiple
definition error (if you happen to use that header in more than one
translation unit).


I've never had any trouble with multiple definition errors in this case, as
long as I have 'inline' at least once somewhere, and I thought that that was
what the standard implied (my 'strange problem' with VS6 was that I had to
place 'inline' in both places or I got a multiply-defined error).
Jul 22 '05 #8
> Works fine on VC7.1. I think for VC7.0 you have to define the operator
in-class.


If I do that, it ignores the cast completely. Thanks for your help, I had
suspected the compiler wasn't up to the job.

Jul 22 '05 #9

"Makhno" <ro**@127.0.0.1 > wrote in message
news:bv******** **@newsg2.svr.p ol.co.uk...
Works fine on VC7.1. I think for VC7.0 you have to define the operator in-class.
If I do that, it ignores the cast completely. Thanks for your help,

I had suspected the compiler wasn't up to the job.


You mean with VC7.0? I'm not surprised it doesn't work, but I don't
understand what you mean by 'ignores the cast completely'.

Jonathan
Jul 22 '05 #10

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

Similar topics

1
3344
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I thought it would be fairly simple to do this exercise, but I encoutered many errors for my...
6
3334
by: Ben Ingram | last post by:
Hi all, I am writing a template matrix class in which the template parameters are the number of rows and number of columns. There are a number of reasons why this is an appropriate tradeoff for my particular application. One of the advantages is that the _compiler_ can force inner matrix dimensions used in multiplication to agree. A _complie-time_ error will be triggered if you write A * B and the number of coluns in A does not equal the...
1
1635
by: Dave Corby | last post by:
Hi all, I have an overloaded template function, and in one particular spot can't get the right version of it to be called. Everywhere else in the program the correct version is called. Here's the function declarations: template <class NumT, class ExpT> NumT rppower (NumT const x, ExpT const y); template <class NumT, class ExpT> SafeInt<NumT> rppower (SafeInt<NumT> const x, SafeInt<ExpT> const y);
3
3939
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. template<class Type> class base { public: base& operator/=(const base&); Type *image;
8
9183
by: David Williams | last post by:
Hi all, I have a templated Vector3D class which holds (x,y,z) components as the specified type. I quite often wish to cast a Vector3D holding ints into a Vector3D holding floats and vice versa. Like so: Vector3D<int> intVec(10,20,30); Vector3D<float> floatVec = intVec; Of course this doesn't work. I would be happy if instead the following
3
2093
by: danilo.horta | last post by:
Hi folks I'm having a scope resolution issue. The gnu compiler is trying to use the "operator function" from derived class rather than from correct one, the base class. // VecBasis.h file template<class T, size_t numDim> class VecBasis { protected:
10
10224
by: mast2as | last post by:
Is it possible to limit a template class to certain types only. I found a few things on the net but nothing seems to apply at compile time. template <typename T> class AClass { public: AClass() {} };
7
2182
by: woessner | last post by:
Hi all, I whipped up a quick class to represent a matrix for use with LAPACK. It's a template class so it can support the 4 data types supported by LAPACK (single/double x complex/real). I added a conversion operator to automatically convert the object to a pointer of the appropriate type. This makes using LAPACK in C++ a lot easier. Unfortunately, it does not work well for the complex data types. The reason is that LAPACK (or,...
3
3758
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and thawed it out. I built a console app using Microsoft Visual C++ 6 (VC++) and it worked great. Only one line in the header file had to be commented out. I built a console app using Borland C++ Builder 5. The linker complained of references to...
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8974
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7500
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.