Template Metaprogramming

B

Bushido Hacks

I learned about Template Metaprogramming (TMP) when I was browsing
Wikipedia a few days ago. According to the article it was an
accidental discovery, but to me it is more like an open opporitunity
to do things with my code that I though would only cause trouble.

The Turing-completeness of TMP makes it a definite must-try-it-out.

I've written up some ideas and posted them on Pastebin. I would like
to know if these are feasible.
http://pastebin.com/4vnapjZ0
 
S

SG

I've written up some ideas and posted them on Pastebin.  I would like
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
 
J

Jorgen Grahn

I learned about Template Metaprogramming (TMP) when I was browsing
Wikipedia a few days ago. According to the article it was an
accidental discovery, but to me it is more like an open opporitunity
to do things with my code that I though would only cause trouble.

Depending on /what/ you do to the code, it /will/ cause trouble. I
have personally not seen a reason to do any template meta-programming
myself (although I do write some templated code now and then).
The Turing-completeness of TMP makes it a definite must-try-it-out.

I've written up some ideas and posted them on Pastebin. I would like
to know if these are feasible.
http://pastebin.com/4vnapjZ0

With all respect: if you have something to say, say it here rather
than on some throwaway URL. Usenet expect to be able to quote, refer
to an unedited copy of what was said earlier etc. The people who
didn't like that style of communication left a decade ago.

/Jorgen
 
B

Bushido Hacks

Depending on /what/ you do to the code, it /will/ cause trouble. I
have personally not seen a reason to do any template meta-programming
myself (although I do write some templated code now and then).

I'd like to use this to create flexible data structures for inputing
various kinds of data. This is all just some stuff I was kicking
around. I plan on buying a new hard drive in the near future so if
all hell breaks loose (which it shouldn't), I will know that it was a
bad idea.
With all respect: if you have something to say, say it here rather
than on some throwaway URL.  Usenet expect to be able to quote, refer
to an unedited copy of what was said earlier etc. The people who
didn't like that style of communication left a decade ago.

/Jorgen

Be happy that it was not some BitLY link where it sends you to some
shady website.

Also destructors are virtual because I feel like it is a good code
practice.
 
B

Bushido Hacks

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++.

Also, template<int N, class T> would NOT work because you are using
"class T" not "typename T".

I took another look at C++ in a Nutshell and found out that this way
of using templates was mentioned all along, only they call it Partial
Specialization and Instantiation. It sounds pretty legitimate.
 
I

Ian Collins

The Vector class I am creating is NOT the vector class that comes with
C++.

Also, template<int N, class T> would NOT work because you are using
"class T" not "typename T".

Why wouldn't it work?
 
B

Bushido Hacks

Right. SG's suggestion was to use std::vector to implement your Vector class.




"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.
 
I

Ian Collins

I see you ignored this correction.

Please trim your replies.
If you hated the code that was posted on Pastebin, you are definitely
going to dislike this bit of code I whipped up.

Then why paste horrible code?
typedef unsigned int unt; // unsigned integer

That sets the tone for what follows!
/* 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

You don't have to qualify all these with said:
Vector<N,T>(const Vector<N,T>&); // Copy Constructor

A reference example of a useless comment!
virtual ~Vector<N,T>(); // Deconstructor, virtual for polymorphism

"Destructor". Do you really want to publicly derive from one of these?
 
L

Luc Danton

I learned about Template Metaprogramming (TMP) when I was browsing
Wikipedia a few days ago. According to the article it was an
accidental discovery, but to me it is more like an open opporitunity
to do things with my code that I though would only cause trouble.

The Turing-completeness of TMP makes it a definite must-try-it-out.

I've written up some ideas and posted them on Pastebin. I would like
to know if these are feasible.
http://pastebin.com/4vnapjZ0

Looking at all your code so far, I fail to see how this qualify as TMP.
Rather, it looks like it's an attempt at generic programming.

Are you familiar with the canonical TMP example?

#include <iostream>

template<int N>
struct factorial {
static const int value = N * factorial<N - 1>::value;
};

template<>
struct factorial<0> {
static const int value = 0;
};

int
main()
{
std::cout << factorial<5>::value << '\n';
}
 
B

Bushido Hacks

Looking at all your code so far, I fail to see how this qualify as TMP.
Rather, it looks like it's an attempt at generic programming.

Are you familiar with the canonical TMP example?

#include <iostream>

template<int N>
struct factorial {
   static const int value = N * factorial<N - 1>::value;

};

template<>
struct factorial<0> {
   static const int value = 0;

};

int
main()
{
   std::cout << factorial<5>::value << '\n';



}

I find that to be a terrible example because it doesn't use a class
structure, nor displays the usage of TMP with Constructors, Copy
constructors, Destructors, and Operators as well as private items.
 
L

Luc Danton

I find that to be a terrible example because it doesn't use a class
structure, nor displays the usage of TMP with Constructors, Copy
constructors, Destructors, and Operators as well as private items.

It can be rewritten as

template<int N>
class factorial {
public:
static const int value = N * factorial<N - 1>::value;
};

if it suits your fancy. Similarly there are no constructors or members
and the like because there never is an instance of factorial in the
program. What good are constructors and private data members if I never
use an instance? You'd need C++0x with constexpr functions, constexpr
constructors and user-defined literals to (possibly) make use of objects
with TMP.
 
L

Luc Danton

It can be rewritten as

template<int N>
class factorial {
public:
static const int value = N * factorial<N - 1>::value;
};

if it suits your fancy. Similarly there are no constructors or members
and the like because there never is an instance of factorial in the
program. What good are constructors and private data members if I never
use an instance? You'd need C++0x with constexpr functions, constexpr
constructors and user-defined literals to (possibly) make use of objects
with TMP.

Actually I went ahead and here is a version that:
- uses the class keyword
- has a constructor
- has a copy constructor
- has a virtual destructor
- has an operator
- has private items
Those are all declared private and not defined to better enforce
encapsulation, too.

template<int N>
class factorial {
public:
static const int value = factorial<N - 1>::value;

private:
factorial();

virtual ~factorial(); // decombobulator

factorial(factorial const&); // copy constructor

// copy assignment op
factorial& operator=(factorial const&);

int member;
double another_member;
float yet_another_member;
};

The specialization for N = 0 is left as an exercise to the reader.
 
J

Jim Langston

Ian Collins said:
On Oct 21, 3:36 pm, Pete Becker<[email protected]> wrote:
[SNIP]
Vector<N,T>(const Vector<N,T>&); // Copy Constructor

A reference example of a useless comment!

Actually, I put that same comment in some code the other day. Why? Because
I almost never work with copy constructors and couldn't remember it's
signature. So maybe it's worthless to you because you work with copy
constructors more.
 
J

Juha Nieminen

Bushido Hacks said:
I find that to be a terrible example because it doesn't use a class
structure, nor displays the usage of TMP with Constructors, Copy
constructors, Destructors, and Operators as well as private items.

Wouldn't that be object-oriented programming rather than template
metaprogramming? Completely different and independent things.
 
S

Stuart Golodetz

Ian Collins said:
On Oct 21, 3:36 pm, Pete Becker<[email protected]> wrote:
[SNIP]
Vector<N,T>(const Vector<N,T>&); // Copy Constructor

A reference example of a useless comment!

Actually, I put that same comment in some code the other day. Why?
Because I almost never work with copy constructors and couldn't remember
it's signature. So maybe it's worthless to you because you work with
copy constructors more.

Actually, I also write "copy constructor", or more specifically:

//########## COPY CONSTRUCTOR & ASSIGNMENT OPERATOR ##########
// <Then the actual things here>

Primarily because I find it useful (more generally than for just the
copy constructor) to have visible watersheds in my class definitions to
break up the code (I find it's great when you're scrolling down :)).
Still not sure whether it's a bad thing to do in general (I know some
people dislike it) -- I guess it depends on your co-workers. It's fairly
minimal-effort to add from my perspective, because I've set up lots of
macros in Visual Studio...

Stu
 
S

Stuart Golodetz

I find that to be a terrible example because it doesn't use a class
structure, nor displays the usage of TMP with Constructors, Copy
constructors, Destructors, and Operators as well as private items.

You find that to be a terrible example primarily because you don't
understand what template metaprogramming is, I suspect.

If you're interested in template metaprogramming, though, you might find
these vaguely interesting:

http://accu.org/index.php/journals/1422
http://accu.org/index.php/journals/1616

I wrote them a while back now, but they're fairly simple to understand.

Stu
 
F

Felix Palmen

* Bushido Hacks said:
Be happy that it was not some BitLY link where it sends you to some
shady website.

Jorgen is right. I'm certainly not the only one not bothering to click
on your link.
Also destructors are virtual because I feel like it is a good code
practice.

It is definitely NOT good practice to (possibly) introduce a vtable for
no reason. Make the destructor virtual exactly when it needs to be
virtual.

Regards,
Felix
 
K

Keith H Duggar

You find that to be a terrible example primarily because you don't
understand what template metaprogramming is, I suspect.

If you're interested in template metaprogramming, though, you might find
these vaguely interesting:

http://accu.org/index.php/journals/1422
http://accu.org/index.php/journals/1616

Indeed it seems quite common to confuse, in various ways
to varying degree, the following distinct concepts:

template programming
generic programming
template meta-programming

The op is just one recent example.

KHD
 
S

Stuart Golodetz

Indeed it seems quite common to confuse, in various ways
to varying degree, the following distinct concepts:

template programming
generic programming
template meta-programming

The op is just one recent example.

KHD

That's completely understandable -- the OP in this case does have rather
a forthright (/slightly rude) way of expressing his misunderstanding of
what's going on, however :) I suspect he needs a book (and possibly a
quiet corner of a library in which to read it).

Regards,
Stu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top