Template casting operator 
July 22nd, 2005, 07:08 AM
| | | |
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>. | 
July 22nd, 2005, 07:08 AM
| | | | re: Template casting operator
"Makhno" <root@127.0.0.1> wrote...[color=blue]
> Why does my cast from Vector<class Float> to Vector<float> not work?[/color]
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.
[color=blue]
> 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;[/color]
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;
[color=blue]
>
> Float x,y,z;
> };
>
> template <class Float> inline Vector<float>
> Vector<Float>::operator() () const
> {
> return Vector<float>((float)x,(float)y,(float)z);
> }[/color]
Make this
template<class F> template<class D>
Vector<F>::operator Vector<D>() const
{
return Vector<D>(x, y, z);
}
[color=blue]
>
>
> int main()
> {
> Vector<double> pd(5.6,3.4,2.4);
>
> Vector<float> pf=(Vector<float>)pd; /* compiler error here */[/color]
Try it now.
[color=blue]
>
> }
>
> I'd ideally like to be able to cast a Vector<double> to a Vector<float>.[/color]
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 | 
July 22nd, 2005, 07:09 AM
| | | | re: Template casting operator
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:43gUb.174338$Rc4.1320258@attbi_s54...[color=blue]
> "Makhno" <root@127.0.0.1> wrote...[color=green]
> > Why does my cast from Vector<class Float> to Vector<float> not[/color][/color]
work?[color=blue]
>[/color]
<snip>
[color=blue]
>
> Back to your problem... You probably wanted to create a templated
> type conversion operator:
>
> template<class S> operator Vector<S>() const;
>[/color]
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 | 
July 22nd, 2005, 07:09 AM
| | | | re: Template casting operator
Did you notice you've defined the operator (),
not the cast operator?
"Makhno" <root@127.0.0.1> wrote in message news:<bvs11g$3hp$1@news6.svr.pol.co.uk>...[color=blue]
> 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>.[/color] | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
> 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)
[color=blue]
> ---------------------------- code that compiles -------------------------[/color]
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. | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
"Makhno" <root@127.0.0.1> wrote...[color=blue][color=green]
> > Declaring something 'inline' without providing its body is useless.[/color]
>
> 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)[/color]
'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).
[color=blue]
>[color=green]
> > ---------------------------- code that[/color][/color]
compiles -------------------------[color=blue]
>
> 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.[/color]
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 | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
"Makhno" <root@127.0.0.1> wrote in message
news:bvu4eo$953$1@newsg3.svr.pol.co.uk...[color=blue][color=green]
> > Declaring something 'inline' without providing its body is[/color][/color]
useless.[color=blue]
>[/color]
[color=blue]
>
> Afraid not. I get "unrecognizable template declaration/definition"[/color]
from[color=blue]
> .NET, then a ton of other errors. But it is the kind of thing I'm[/color]
looking[color=blue]
> for, just didn't know how to get the syntax correct.
>[/color]
Works fine on VC7.1. I think for VC7.0 you have to define the operator
in-class.
Jonathan | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
> Supplying 'inline' with the definition is perfectly fine and is usually[color=blue]
> 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).[/color]
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). | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
> Works fine on VC7.1. I think for VC7.0 you have to define the operator[color=blue]
> in-class.[/color]
If I do that, it ignores the cast completely. Thanks for your help, I had
suspected the compiler wasn't up to the job. | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
"Makhno" <root@127.0.0.1> wrote in message
news:bvu6hg$b0m$2@newsg2.svr.pol.co.uk...[color=blue][color=green]
> > Works fine on VC7.1. I think for VC7.0 you have to define the[/color][/color]
operator[color=blue][color=green]
> > in-class.[/color]
>
> If I do that, it ignores the cast completely. Thanks for your help,[/color]
I had[color=blue]
> suspected the compiler wasn't up to the job.
>[/color]
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 | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
> You mean with VC7.0? I'm not surprised it doesn't work, but I don't[color=blue]
> understand what you mean by 'ignores the cast completely'.[/color]
As in 'reacts as though I had never written the cast operator to the Vector
class'. | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
"Makhno" <root@127.0.0.1> wrote in message
news:bvu80s$4vi$1@news7.svr.pol.co.uk...[color=blue][color=green]
> > You mean with VC7.0? I'm not surprised it doesn't work, but I[/color][/color]
don't[color=blue][color=green]
> > understand what you mean by 'ignores the cast completely'.[/color]
>
> As in 'reacts as though I had never written the cast operator to the[/color]
Vector[color=blue]
> class'.
>[/color]
Yes, that's nasty. Did you try using converting constructors instead?
Jonathan | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
"Jonathan Turkanis" <technews@kangaroologic.com> wrote in message:
[color=blue]
> Yes, that's nasty. Did you try using converting constructors[/color]
instead?
You probably need a templated assignment operator too, if you do this.
Jonathan | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
> Did you try using converting constructors instead?
Yes, worked straight away. You're right - perhaps I don't need the cast
operator at all.
[color=blue]
> You probably need a templated assignment operator too, if you do this.[/color]
No, as
Vector<float> pf=pd;
and
Vector<float> pf(pd);
are equivalent; | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
"Makhno" <root@127.0.0.1> wrote in message
news:bvu9lt$lk$1@newsg4.svr.pol.co.uk...[color=blue][color=green]
> > Did you try using converting constructors instead?[/color]
>
> Yes, worked straight away. You're right - perhaps I don't need the[/color]
cast[color=blue]
> operator at all.
>[color=green]
> > You probably need a templated assignment operator too, if you do[/color][/color]
this.[color=blue]
>
> No, as
> Vector<float> pf=pd;
> and
> Vector<float> pf(pd);
> are equivalent;
>[/color]
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 | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
"Jonathan Turkanis" <technews@kangaroologic.com> wrote in message
news:bvua5m$t9f91$1@ID-216073.news.uni-berlin.de...[color=blue]
>
> "Makhno" <root@127.0.0.1> wrote in message
> news:bvu9lt$lk$1@newsg4.svr.pol.co.uk...[color=green][color=darkred]
> > > Did you try using converting constructors instead?[/color]
> >
> > Yes, worked straight away. You're right - perhaps I don't need the[/color]
> cast[color=green]
> > operator at all.
> >[color=darkred]
> > > You probably need a templated assignment operator too, if you do[/color][/color]
> this.[color=green]
> >
> > No, as
> > Vector<float> pf=pd;
> > and
> > Vector<float> pf(pd);
> > are equivalent;
> >[/color][/color]
[color=blue]
>
> 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.
>[/color]
I spoke too soon. It's non needed here either. But it's not because
of copy initialization.
Jonathan | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
> But that's just one use of the implicit conversion.[color=blue]
>
> How about:
>
> Vector<double> pd;
> Vector<float> pf;
> // More stuff here.
> pf = pd;
>
> ?[/color]
Hmm, this still seems to work, how peculiar. Vector<> doesn't have an
assignment operator. | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
> I spoke too soon. It's non needed here either. But it's not because[color=blue]
> of copy initialization.[/color]
why then? | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
"Jonathan Turkanis" <technews@kangaroologic.com> wrote...[color=blue]
>
> "Makhno" <root@127.0.0.1> wrote in message
> news:bvu9lt$lk$1@newsg4.svr.pol.co.uk...[color=green][color=darkred]
> > > Did you try using converting constructors instead?[/color]
> >
> > Yes, worked straight away. You're right - perhaps I don't need the[/color]
> cast[color=green]
> > operator at all.
> >[color=darkred]
> > > You probably need a templated assignment operator too, if you do[/color][/color]
> this.[color=green]
> >
> > No, as
> > Vector<float> pf=pd;
> > and
> > Vector<float> pf(pd);
> > are equivalent;
> >[/color]
>
> 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.[/color]
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 | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:37yUb.181415$Rc4.1339653@attbi_s54...[color=blue]
> "Jonathan Turkanis" <technews@kangaroologic.com> wrote...[color=green]
> >[/color][/color]
[color=blue]
> 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.
>[/color]
You're not -- I was :(.
Jonathan | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
"Makhno" <root@127.0.0.1> wrote in message
news:bvuak9$253$1@newsg1.svr.pol.co.uk...[color=blue][color=green]
> > I spoke too soon. It's non needed here either. But it's not[/color][/color]
because[color=blue][color=green]
> > of copy initialization.[/color]
>
> why then?
>[/color]
See Victor's post.
Jonathan | 
July 22nd, 2005, 07:11 AM
| | | | re: Template casting operator
"Makhno" <root@127.0.0.1> wrote...[color=blue][color=green]
> > But that's just one use of the implicit conversion.
> >
> > How about:
> >
> > Vector<double> pd;
> > Vector<float> pf;
> > // More stuff here.
> > pf = pd;
> >
> > ?[/color]
>
> Hmm, this still seems to work, how peculiar. Vector<> doesn't have an
> assignment operator.[/color]
Yes, it does. If you don't declare one yourself, the compiler will
do it for you. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,720 network members.
|