Template spec question

S

suranap

I am looking at some examples of template metaprogramming, but I'm not
clear on some details of templates. In particular, what kinds of
computations will be performed at compile-time? Is it only arithmetic
on integers (+,-,*,/)?

1) When using a template, what kinds of operations are allowed within
the <>?

int x = Factorial<i-1>::value // what other operators are allowed
besides -?

2) Within an enum, what operators are allowed to compute a value at
compile-time?

enum { value = N * Factorial<N-1>::value }; // instead of *? Will other
datatypes work, like char or strings?

3) Will the value of an enum get propagated within the template body at
compile-time?

template<int N>
struct Example {
enum { value1 = N - 1 };
enum { value2 = value1 - 1 } ; // value2 needs value1 here
}



Example:

template<int N>
struct Factorial {
enum { value = N * Factorial<N-1>::value };
};

template<>
struct Factorial<1> {
enum { value = 1 };
};

// Example use
const int fact5 = Factorial<5>::value;
 
J

Jonathan Mcdougall

I am looking at some examples of template metaprogramming, but I'm not

Get Modern C++ Design (Andrei Alexandrescu) and
C++ templates (Josuttis/Vandervoode)
clear on some details of templates. In particular, what kinds of
computations will be performed at compile-time? Is it only arithmetic
on integers (+,-,*,/)?

Everything which may be calculated at compile-time, which means all
constants.

template <int i>
class C
{
char a;
};

int main()
{
1) When using a template, what kinds of operations are allowed within
the <>?

int x = Factorial<i-1>::value // what other operators are allowed
besides -?

Anything which produces a compile-time constant, usually something you
can calculate yourself on a piece of paper.
2) Within an enum, what operators are allowed to compute a value at
compile-time?

enum { value = N * Factorial<N-1>::value }; // instead of *?

Same as above.
Will other
datatypes work, like char or strings?

Depends on what you mean here. Where would they be used? If you mean in
the template list such as

template <std::string s>
class C {};

well that's illegal. non-type template parameters must be built-ins.
Classes can seldom be evaluated at compile-time, except for very simple
ones. Forbiding their use in template parameter lists is common sense.
3) Will the value of an enum get propagated within the template body at
compile-time?

template<int N>
struct Example {
enum { value1 = N - 1 };
enum { value2 = value1 - 1 } ; // value2 needs value1 here

}

Of course.
Example:

template<int N>
struct Factorial {
enum { value = N * Factorial<N-1>::value };

};

template<>
struct Factorial<1> {
enum { value = 1 };

};

// Example use
const int fact5 = Factorial<5>::value;

That's perfectly kosher.


Jonathan
 

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

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,101
Latest member
MarcusSkea
Top