By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,902 Members | 4,554 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,902 IT Pros & Developers. It's quick & easy.

Template spec question

P: n/a
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;

Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
> 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[i];
};

int main()
{
C<10*2 << 2 + 4 / 1> c;
}
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

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.