Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old December 24th, 2005, 08:55 PM
Danilo Horta
Guest
 
Posts: n/a
Default 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:

Expand|Select|Wrap|Line Numbers
  1. template<class T> class Vec : public VecBasis<T> {
  2. public:
  3. Vec<T>() : VecBasis<T>() {}
  4. Vec<T>(T a, T b, T c) : VecBasis<T>(a, b, c) {}
  5. Vec<T>(const Vec<T>& v) : VecBasis<T>(v) {}
  6.  
  7. };
  8.  
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:

Expand|Select|Wrap|Line Numbers
  1. class Vec : public Vec<float> {
  2. public:
  3. Vec() : Vec<float>() {}
  4. Vec(float a, float b, float c) : Vec<float>(a, b, c) {}
  5. };
  6.  
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

  #2  
Old December 24th, 2005, 10:45 PM
PKH
Guest
 
Posts: n/a
Default 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:
>
>
Expand|Select|Wrap|Line Numbers
  1. > template<class T> class Vec : public VecBasis<T> {
  2. > public:
  3. > Vec<T>() : VecBasis<T>() {}
  4. > Vec<T>(T a, T b, T c) : VecBasis<T>(a, b, c) {}
  5. > Vec<T>(const Vec<T>& v) : VecBasis<T>(v) {}
  6. >
  7. > };
>
> 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:
>
>
Expand|Select|Wrap|Line Numbers
  1. > class Vec : public Vec<float> {
  2. > public:
  3. > Vec() : Vec<float>() {}
  4. > Vec(float a, float b, float c) : Vec<float>(a, b, c) {}
  5. > };
>
> 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





  #3  
Old December 25th, 2005, 01:45 AM
Danilo Horta
Guest
 
Posts: n/a
Default 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

  #4  
Old December 25th, 2005, 02:35 AM
BobR
Guest
 
Posts: n/a
Default 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?:

Expand|Select|Wrap|Line Numbers
  1. template<class T = double> class Vec : public VecBasis<T> {
  2. public:
  3. Vec<T>() : VecBasis<T>() {}
  4. Vec<T>(T a, T b, T c) : VecBasis<T>(a, b, c) {}
  5. Vec<T>(const Vec<T>& v) : VecBasis<T>(v) {}
  6. };
  7.  
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


  #5  
Old December 25th, 2005, 04:05 AM
Danilo Horta
Guest
 
Posts: n/a
Default Re: Default Template Parameters

BobR wrote:[color=blue]
> You do know that you can do the following, right?:
>
>
Expand|Select|Wrap|Line Numbers
  1. > template<class T = double> class Vec : public VecBasis<T> {
  2. >    public:
  3. >     Vec<T>() : VecBasis<T>() {}
  4. >     Vec<T>(T a, T b, T c) : VecBasis<T>(a, b, c) {}
  5. >     Vec<T>(const Vec<T>& v) : VecBasis<T>(v) {}
  6. >     };
>
> 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

  #6  
Old December 25th, 2005, 04:55 AM
Luke Meyers
Guest
 
Posts: n/a
Default 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

  #7  
Old December 25th, 2005, 05:45 PM
Danilo Horta
Guest
 
Posts: n/a
Default 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

 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

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.
Post your question now . . .
It's fast and it's free

Popular Articles