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 6 1666
"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:
- 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
I reccommend useing typedefs for template types, f.ex typedef vec<float>
FVec.
PKH
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
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?: -
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
BobR wrote: 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
Yes, but Vec<> isn't very sexy ;P
Cheers
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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>...
|
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>...
|
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...
|
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...
|
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...
|
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...
|
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)();
|
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...
|
by: William Xu |
last post by:
Compiling:
template <class T = int>
T foo(const T& t)
{}
int main(int argc, char *argv)
{}
gcc complains:
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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
|
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...
|
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,...
|
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...
| |