
December 24th, 2005, 08:55 PM
| | | Default Template Parameters
Hi Folks
I'm making a class Vec (aka Vector) using template and operator
overloading just to use what I'm learning from the Stroustrup's book.
See: -
template<class T> class Vec : public VecBasis<T> {
-
public:
-
Vec<T>() : VecBasis<T>() {}
-
Vec<T>(T a, T b, T c) : VecBasis<T>(a, b, c) {}
-
Vec<T>(const Vec<T>& v) : VecBasis<T>(v) {}
-
-
};
-
So the user can do: Vec<float> v;
But I want a default template parameter without the necessity of user
to declare Vec<> v; , using <>. That is, a way that puts float as
template argument when one declares Vec v;
I tried the following: -
class Vec : public Vec<float> {
-
public:
-
Vec() : Vec<float>() {}
-
Vec(float a, float b, float c) : Vec<float>(a, b, c) {}
-
};
-
However, the compiler insists to use the class template version when I
declare Vec v;. Changing the Vec name of the latter definition to V
and using V v; , all goes right. But I really want to use Vec v;
Is there a way? Am I doing a mistake?
Thanks in advance | 
December 24th, 2005, 10:45 PM
| | | Re: Default Template Parameters
"Danilo Horta" <danilo.horta@gmail.com> wrote in message
news:1135457337.609183.29070@z14g2000cwz.googlegro ups.com...[color=blue]
> Hi Folks
>
>
> I'm making a class Vec (aka Vector) using template and operator
> overloading just to use what I'm learning from the Stroustrup's book.
> See:
>
> -
> template<class T> class Vec : public VecBasis<T> {
-
> public:
-
> Vec<T>() : VecBasis<T>() {}
-
> Vec<T>(T a, T b, T c) : VecBasis<T>(a, b, c) {}
-
> Vec<T>(const Vec<T>& v) : VecBasis<T>(v) {}
-
>
-
> };
-
>
>
> So the user can do: Vec<float> v;
> But I want a default template parameter without the necessity of user
> to declare Vec<> v; , using <>. That is, a way that puts float as
> template argument when one declares Vec v;
>
> I tried the following:
>
> -
> class Vec : public Vec<float> {
-
> public:
-
> Vec() : Vec<float>() {}
-
> Vec(float a, float b, float c) : Vec<float>(a, b, c) {}
-
> };
-
>
>
> However, the compiler insists to use the class template version when I
> declare Vec v;. Changing the Vec name of the latter definition to V
> and using V v; , all goes right. But I really want to use Vec v;
>
> Is there a way? Am I doing a mistake?
>
> Thanks in advance
>[/color]
I reccommend useing typedefs for template types, f.ex typedef vec<float>
FVec.
PKH | 
December 25th, 2005, 01:45 AM
| | | Re: Default Template Parameters
PKH wrote:[color=blue]
>
> I reccommend useing typedefs for template types, f.ex typedef vec<float>
> FVec.
>
> PKH[/color]
Seems that I'll have to typedef Vec<float> vec; like the
basic_string/string does.
Thanks anyway | 
December 25th, 2005, 02:35 AM
| | | Re: Default Template Parameters
Danilo Horta wrote in message
<1135474570.977199.15190@o13g2000cwo.googlegroups. com>...[color=blue]
>
>PKH wrote:[color=green]
>> I reccommend useing typedefs for template types, f.ex typedef vec<float>
>> FVec.
>> PKH[/color]
>
>Seems that I'll have to typedef Vec<float> vec; like the
>basic_string/string does.
>
>Thanks anyway[/color]
You do know that you can do the following, right?: -
template<class T = double> class Vec : public VecBasis<T> {
-
public:
-
Vec<T>() : VecBasis<T>() {}
-
Vec<T>(T a, T b, T c) : VecBasis<T>(a, b, c) {}
-
Vec<T>(const Vec<T>& v) : VecBasis<T>(v) {}
-
};
-
Vec<int> MyVeci;
Vec<> MyVecDbl;
[ I seldom use templates, so I'm not sure it always works. It did work on my
GCC(MinGW) 3.3.1. ]
--
Bob R
POVrookie | 
December 25th, 2005, 04:05 AM
| | | Re: Default Template Parameters
BobR wrote:[color=blue]
> You do know that you can do the following, right?:
>
> -
> template<class T = double> class Vec : public VecBasis<T> {
-
> public:
-
> Vec<T>() : VecBasis<T>() {}
-
> Vec<T>(T a, T b, T c) : VecBasis<T>(a, b, c) {}
-
> Vec<T>(const Vec<T>& v) : VecBasis<T>(v) {}
-
> };
-
>
>
> Vec<int> MyVeci;
> Vec<> MyVecDbl;
>
> [ I seldom use templates, so I'm not sure it always works. It did work on my
> GCC(MinGW) 3.3.1. ]
> --
> Bob R
> POVrookie[/color]
Yes, but Vec<> isn't very sexy ;P
Cheers | 
December 25th, 2005, 04:55 AM
| | | Re: Default Template Parameters
I don't think you really want to be able to do what you think you want
to be able to do. Consider for a moment the analogous case of default
function arguments. Let's say I had the following function defined:
int doStuff(int x = 0);
Now, would you want the following to be valid?
int a = doStuff; // (1)
As opposed to, of course:
int a = doStuff();
I think most C++ programmers would be glad that the standard does not
permit constructions such as (1). The name of a function and its
return value are two different things, and just because you've got
default arguments doesn't mean you want to totally change up the
syntax.
Now, consider the following analogy:
function name : function return value :: template name :: template
instantiation
A template is "invoked" (instantiated, specialized...) and the "return
value" is a type. Class templates are not the same sort of entity as
classes -- they are a mechanism which produces classes. In light of
this, I think using "Vec<>" is exactly as consistent, satisfying, etc.
as using "doStuff()." The analogy with a function call is pretty
clear. Think of all the cases in which omitting parentheses on a
function call would lead to confusion, and consider how basically the
same reasoning applies to template instantiation.
Luke | 
December 25th, 2005, 05:45 PM
| | | Re: Default Template Parameters
Luke Meyers wrote:[color=blue]
> I don't think you really want to be able to do what you think you want
> to be able to do. Consider for a moment the analogous case of default
> function arguments. Let's say I had the following function defined:
>
> int doStuff(int x = 0);
>
> Now, would you want the following to be valid?
>
> int a = doStuff; // (1)
>
> As opposed to, of course:
>
> int a = doStuff();
>
> I think most C++ programmers would be glad that the standard does not
> permit constructions such as (1). The name of a function and its
> return value are two different things, and just because you've got
> default arguments doesn't mean you want to totally change up the
> syntax.
>
> Now, consider the following analogy:
>
> function name : function return value :: template name :: template
> instantiation
>
> A template is "invoked" (instantiated, specialized...) and the "return
> value" is a type. Class templates are not the same sort of entity as
> classes -- they are a mechanism which produces classes. In light of
> this, I think using "Vec<>" is exactly as consistent, satisfying, etc.
> as using "doStuff()." The analogy with a function call is pretty
> clear. Think of all the cases in which omitting parentheses on a
> function call would lead to confusion, and consider how basically the
> same reasoning applies to template instantiation.
>
> Luke[/color]
I totally agree with you. And typedef Vec<float> vec; seems to be a
nice way to create a "default class type" for the Vec<> class template.
Thanks |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
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 network members.
|