Re: Template Metaprogramming
On Oct 21, 3:36 pm, Pete Becker <p...@versatilecoding.com> wrote:
On 2010-10-21 16:25:57 -0400, Bushido Hacks said:
On Oct 21, 12:16 pm, SG <s.gesem...@gmail.com> wrote:
On 21 Okt., 19:05, Bushido Hacks wrote:
I've written up some ideas and posted them on Pastebin. I would li=
ke
to know if these are feasible.http://pastebin.com/4vnapjZ0
I don't see anything in there that would qualify as template meta-
programming. But I wonder why you made the destructor virtual. I also
wonder why you use the dimension N as template parameter and at the
same time use dynamically allocated arrays and a pointer member for
the vectors' coefficients. Also, if you really need dynamically
allocated coefficients you could just use a std::vector for this:
template<int N, class T>
class vec {
std::vector<T> coefficients;
public:
vec() : coefficients(N) {}
...
};
This way you don't need user-defined copy ctor, assignment operator
nor a destructor.
Cheers!
SG
The Vector class I am creating is NOT the vector class that comes with
C++.
Right. SG's suggestion was to use std::vector to implement your Vector cl=
ass.
Also, template<int N, class T> would NOT work because you are using
"class T" not "typename T".
"class T" works just fine. In this context, they're interchangable.
Some people think "class T" is confusing because T doesn't actually
have to be a class, but that's too pedantic for my taste.
--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)
If you hated the code that was posted on Pastebin, you are definitely
going to dislike this bit of code I whipped up.
typedef unsigned int unt; // unsigned integer
/* Superprimary template? */
template <unt N, typename T, unt M, typename S>
class Vector<N,T>
{
public:
Vector<N,T>(); // All elements in T* have a numeric value of
zero
Vector<N,T>(const T*); // There should be N number of elements in
T*, otherwise they are set to zero. If there are more than N
elements, they are ignored.
Vector<N,T>(const S*); // If S is of a different type, but still
has N number of elements, convert the S values to T type and fill in
the blanks with zeros.
Vector<N,T>(const Vector<N,T>&); // Copy Constructor
Vector<N,T>(const Vector<M,T>&); // Copy Constructor, same type but
different number of elements. If N > M, the blanks are filled with
zeros. If N < M, only the first values Vector<M,T> are put into
Vector<N,T>. If N = M, it should just call the copy constructor.
Vector<N,T>(const Vector<M,S>&); // Copy Constructor, different
number of elements and different types. Same rules as the previous
copy constructor but with some typecasting.
virtual ~Vector<N,T>(); // Deconstructor, virtual for polymorphism
// Eventually, define the == and != ?
Vector<N,T>& operator=(const T*);
Vector<N,T>& operator=(const S*);
Vector<N,T>& operator=(const Vector<N,T>&);
Vector<N,T>& operator=(const Vector<M,T>&);
Vector<N,T>& operator=(const Vector<M,S>&);
Vecotr<N,T>& set(const unt,const T&); // set a value in the Vector
Vector<N,T>& set(const unt,const S&); // set a value in the Vector
that is of a different type. (Assuming this is possible. This will
typecast the S value to a T value)
T get(const unt) const; // get a specific value
T* get() const; // get the set of values from a vector.
//unt index(const T&) const; // find the first instance of a
specific value.
void print() const; // print out the values of the data
structure
//void print(std::fout) const; // Print the values to some file.
(Eventually I'll write this up.)
private:
unt i = N; // number of elements
T* t; // list of elements
}
Remember, I'm not using the C++ vector class. This is something I'm
working on to work with numerical data structures for mathematic and
scientific models.