473,387 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

Dec 24 '05 #1
6 1666
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

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

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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Patrick Kowalzick | last post by:
Dear all, I have a question about default template parameters. I want to have a second template parameter which as a default parameter, but depends on the first one (see below). Is something...
2
by: Michael Stembera | last post by:
I would like to use default parameters in nested templates but MS VC++ 7.1 chokes on it. Does anyone know how to fix the simple example below or if indeed it is possible? template <int N=7>...
11
by: BRG | last post by:
I know that default template arguments cannot be used in function templates but are default function parameters legal? That is, is this: ---------------------------------- #include <functional>...
12
by: aaragon | last post by:
Hello all. I have a simple question that seems trivial but I can't make it to work. I have a class that takes as a template argument, another class. The idea is as follows: #include...
5
by: Yan | last post by:
I have the following code that the compiler complains about that "No parameters provided for template". The compiler is the one that comes with Sun Solaris, not sure what exactly. I have no control...
6
by: Mark | last post by:
I have a problem with a template class defined: // start matrix.h file template <class Tclass Matrix { public: Matrix() { // default constructor } Matrix(const Subscript rows, const...
2
by: Nike | last post by:
I have a small question w.r.t usage of default arguements in template.. I shall try to elaborate this with an example.. let's say I have some template function , where EntryType is the input for...
12
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
8
by: William Xu | last post by:
Compiling: template <class T = int> T foo(const T& t) {} int main(int argc, char *argv) {} gcc complains:
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.