473,320 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Template spec question

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
1 1242
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Alexander Stippler | last post by:
Hi, what do I have to do to get this (incorrect) piece of code to work. The specialization is wrong, but how can I do it? template <typename T, typename V> class Mask { public: Mask(int i)...
5
by: Amit | last post by:
Greetings all, I am writing some code somehwat similar to the test code I have below. I am having a variety of issues with template specialization. I am not sure if this is related to something...
1
by: Jon Slaughter | last post by:
I've managed to put together a template class that basicaly creates a recursive tree that lets you easily specify the "base" class of that tree and and ending notes and lets you stop the recursive...
6
by: Stuart McGraw | last post by:
I am looking for a VBA "format" or "template" function, that is, a function that takes a format string and a varying number of arguments, and substitutes the argument values into the format string...
1
by: Joseph Turian | last post by:
How can I specialize the value of only one template parameter? Here's the fragment of code I'd like to get working: typedef enum {START, PARENT, CHILD, END} locator_ty; template <locator_ty L,...
2
by: Imre | last post by:
Hi I'd like to know what the problem is with the following code. I've tried compiling it with two compilers (VC++ 8.0, and Comeau online compiler), and both failed to compile it, saying that...
7
by: Markus Petermann | last post by:
Hello, I have a small problem I want to demonstrate with a small demo program: -------------- Snip -------------- #include <string> namespace {
6
by: year1943 | last post by:
For template <typename Tclass My ; I can define partial spec-ns somewhat like template <typename Tclass My<T*; or template <typename Tclass My<Another<T ; And full spec-n, say template <class...
1
by: jason.cipriani | last post by:
Here is an example with 3 files, containing a template structure and also a template function. The header A.h declares a template structure A with a default (i.e. for any template parameter),...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.