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

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<float>)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 2104
"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>::operator Vector<D>() const
{
return Vector<D>(x, y, z);
}


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

Vector<float> pf=(Vector<float>)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>::operator 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.********@comAcast.net> wrote in message
news:43gUb.174338$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.pol.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<float>)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 "unrecognizable 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 "unrecognizable 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.pol.co.uk...
Declaring something 'inline' without providing its body is
useless.
Afraid not. I get "unrecognizable 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.pol.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
> 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'.


As in 'reacts as though I had never written the cast operator to the Vector
class'.
Jul 22 '05 #11

"Makhno" <ro**@127.0.0.1> wrote in message
news:bv**********@news7.svr.pol.co.uk...
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'.
As in 'reacts as though I had never written the cast operator to the

Vector class'.


Yes, that's nasty. Did you try using converting constructors instead?

Jonathan
Jul 22 '05 #12

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message:
Yes, that's nasty. Did you try using converting constructors

instead?

You probably need a templated assignment operator too, if you do this.

Jonathan


Jul 22 '05 #13
> Did you try using converting constructors instead?

Yes, worked straight away. You're right - perhaps I don't need the cast
operator at all.
You probably need a templated assignment operator too, if you do this.


No, as
Vector<float> pf=pd;
and
Vector<float> pf(pd);
are equivalent;
Jul 22 '05 #14

"Makhno" <ro**@127.0.0.1> wrote in message
news:bv*********@newsg4.svr.pol.co.uk...
Did you try using converting constructors instead?
Yes, worked straight away. You're right - perhaps I don't need the

cast operator at all.
You probably need a templated assignment operator too, if you do
this.
No, as
Vector<float> pf=pd;
and
Vector<float> pf(pd);
are equivalent;


But that's just one use of the implicit conversion.

How about:

Vector<double> pd;
Vector<float> pf;
// More stuff here.
pf = pd;

?

I didn't suggest a templated assignement operator first because your
example didn't require it. But if you want a general-purpose
substitute for the conversion operator, you need it.

Jonathan

Jul 22 '05 #15

"Jonathan Turkanis" <te******@kangaroologic.com> wrote in message
news:bv************@ID-216073.news.uni-berlin.de...

"Makhno" <ro**@127.0.0.1> wrote in message
news:bv*********@newsg4.svr.pol.co.uk...
Did you try using converting constructors instead?
Yes, worked straight away. You're right - perhaps I don't need the

cast
operator at all.
You probably need a templated assignment operator too, if you do

this.

No, as
Vector<float> pf=pd;
and
Vector<float> pf(pd);
are equivalent;


I didn't suggest a templated assignement operator first because your
example didn't require it. But if you want a general-purpose
substitute for the conversion operator, you need it.


I spoke too soon. It's non needed here either. But it's not because
of copy initialization.

Jonathan

Jul 22 '05 #16
> But that's just one use of the implicit conversion.

How about:

Vector<double> pd;
Vector<float> pf;
// More stuff here.
pf = pd;

?


Hmm, this still seems to work, how peculiar. Vector<> doesn't have an
assignment operator.
Jul 22 '05 #17
> I spoke too soon. It's non needed here either. But it's not because
of copy initialization.


why then?
Jul 22 '05 #18
"Jonathan Turkanis" <te******@kangaroologic.com> wrote...

"Makhno" <ro**@127.0.0.1> wrote in message
news:bv*********@newsg4.svr.pol.co.uk...
Did you try using converting constructors instead?


Yes, worked straight away. You're right - perhaps I don't need the

cast
operator at all.
You probably need a templated assignment operator too, if you do

this.

No, as
Vector<float> pf=pd;
and
Vector<float> pf(pd);
are equivalent;


But that's just one use of the implicit conversion.

How about:

Vector<double> pd;
Vector<float> pf;
// More stuff here.
pf = pd;

?

I didn't suggest a templated assignement operator first because your
example didn't require it. But if you want a general-purpose
substitute for the conversion operator, you need it.


Why? 'pf = pd' expression will result into the use of compiler-
generated assignment operator and a construction of a temporary
on the right side, no? It should be equivalent to

{ Vector<float> temp(pd); pf = temp; }

No special assignment operator is needed here.

Correct me if I am wrong.

V
Jul 22 '05 #19

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:37yUb.181415$Rc4.1339653@attbi_s54...
"Jonathan Turkanis" <te******@kangaroologic.com> wrote...
Why? 'pf = pd' expression will result into the use of compiler-
generated assignment operator and a construction of a temporary
on the right side, no? It should be equivalent to

{ Vector<float> temp(pd); pf = temp; }

No special assignment operator is needed here.

Correct me if I am wrong.


You're not -- I was :(.

Jonathan
Jul 22 '05 #20

"Makhno" <ro**@127.0.0.1> wrote in message
news:bv**********@newsg1.svr.pol.co.uk...
I spoke too soon. It's non needed here either. But it's not because of copy initialization.


why then?


See Victor's post.

Jonathan
Jul 22 '05 #21
"Makhno" <ro**@127.0.0.1> wrote...
But that's just one use of the implicit conversion.

How about:

Vector<double> pd;
Vector<float> pf;
// More stuff here.
pf = pd;

?


Hmm, this still seems to work, how peculiar. Vector<> doesn't have an
assignment operator.


Yes, it does. If you don't declare one yourself, the compiler will
do it for you.
Jul 22 '05 #22

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

Similar topics

1
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...
6
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...
1
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...
3
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. ...
8
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....
3
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...
10
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:...
7
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...
3
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...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.