By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,995 Members | 1,079 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,995 IT Pros & Developers. It's quick & easy.

Template casting operator

P: n/a
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
Share this Question
Share on Google+
21 Replies


P: n/a
"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

P: n/a

"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

P: n/a
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

P: n/a
> 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

P: n/a
"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

P: n/a

"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

P: n/a
> 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

P: n/a
> 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

P: n/a

"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

P: n/a
> 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

P: n/a

"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

P: n/a

"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

P: n/a
> 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

P: n/a

"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

P: n/a

"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

P: n/a
> 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

P: n/a
> I spoke too soon. It's non needed here either. But it's not because
of copy initialization.


why then?
Jul 22 '05 #18

P: n/a
"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

P: n/a

"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

P: n/a

"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

P: n/a
"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 discussion thread is closed

Replies have been disabled for this discussion.