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

Default Template Parameters

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

Dec 24 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
PKH

"Danilo Horta" <da**********@gmail.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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.  };
  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


I reccommend useing typedefs for template types, f.ex typedef vec<float>
FVec.

PKH

Dec 24 '05 #2

P: n/a

PKH wrote:

I reccommend useing typedefs for template types, f.ex typedef vec<float>
FVec.

PKH


Seems that I'll have to typedef Vec<float> vec; like the
basic_string/string does.

Thanks anyway

Dec 25 '05 #3

P: n/a

Danilo Horta wrote in message
<11*********************@o13g2000cwo.googlegroups. com>...

PKH wrote:
I reccommend useing typedefs for template types, f.ex typedef vec<float>
FVec.
PKH


Seems that I'll have to typedef Vec<float> vec; like the
basic_string/string does.

Thanks anyway


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
Dec 25 '05 #4

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


Yes, but Vec<> isn't very sexy ;P

Cheers

Dec 25 '05 #5

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

Dec 25 '05 #6

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


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

Dec 25 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.