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

condition on template variables

P: n/a
Hello,

I have a question concerning the template classes and their parameters.
Is it possible to set a condition on the template parameters in a way
that could block the compilation if the condition isn't true?
(e.g. by throwing an error message to the compiler).
And is it possible to use this type of condition to make the compiler
choose the best method (between some others).

Here is an example to illustrate my question:
imagine that my condition is "the variable nb is greater than 1"

template < class T, int nb >
class Foo
{
private:

T _tab[nb];

public:

Foo(T[nb]);

...

}
typedef Foo<float,2> Coord; // this line must compile

typedef Foo<int,3> Example; // this line must compile too

typedef Foo<float,0> Another; // but I want this line to throw an error
to the compiler

// This example illustrate my first question
My second one is about a condition that could choose a method to
compile, during the compilation.

For example, here is what i would expect:

template < class T, int nb >
class Foo2
{
private:

T _tab[nb];

public:

#if( nb==1 )
Foo(T t1);

#else if (nb==2)
Foo(T t1, T t2);

#else if....

...

}

// Of course, this syntax is incorrect because "nb" is unknown by the
"precompiler" before compilation.
I'd like to find some kind of condition on template variables at
compilation time.

If you have an idea, it would be a great help.

Thank you

Yohan Firmy
Feb 22 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Yohan wrote:
I have a question concerning the template classes and their
parameters. Is it possible to set a condition on the template
parameters in a way that could block the compilation if the condition
isn't true? (e.g. by throwing an error message to the compiler).
Yes, usually. It's called "compile-time assert" (or "assertion").
And is it possible to use this type of condition to make the compiler
choose the best method (between some others).
Yes, but that's not the preferred technique.
Here is an example to illustrate my question:
imagine that my condition is "the variable nb is greater than 1"

template < class T, int nb >
class Foo
{
private:

T _tab[nb];

public:

Foo(T[nb]);
This declaration looks fishy.

...

}
typedef Foo<float,2> Coord; // this line must compile

typedef Foo<int,3> Example; // this line must compile too

typedef Foo<float,0> Another; // but I want this line to throw an
error to the compiler
It will because zero-sized arrays are not allowed. But not until you
actually try using 'Another'. A simple "typedef" does not cause the
template to be instantiated. Only an attempt to instantiate your
template will cause the compiler to try to allocate the array of size
'nb'.
// This example illustrate my first question
My second one is about a condition that could choose a method to
compile, during the compilation.

For example, here is what i would expect:

template < class T, int nb >
class Foo2
{
private:

T _tab[nb];

public:

#if( nb==1 )
Foo(T t1);

#else if (nb==2)
Foo(T t1, T t2);

#else if....

...

}

// Of course, this syntax is incorrect because "nb" is unknown by the
"precompiler" before compilation.
Template _specialisations_ are in the language exactly for that.
I'd like to find some kind of condition on template variables at
compilation time.

If you have an idea, it would be a great help.


Yes, plently of opportunities exist. Perhaps you should simply devote
some time to _learning_ templates and what they offer instead of making
guesses and asking for hints or ideas. I strongly recommend the book
"C++ Templates" by Vandevoorde and Josuttis.

V
--
Please remove capital As from my address when replying by mail
Feb 22 '06 #2

P: n/a
Yohan wrote:
Hello,

I have a question concerning the template classes and their parameters.
Is it possible to set a condition on the template parameters in a way
that could block the compilation if the condition isn't true?


Boost uses this technique a lot:

http://www.boost.org/doc/html/boost_staticassert.html
http://www.boost.org/doc/html/boost_.../category.html

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 22 '06 #3

P: n/a
Yohan <yo*****@gmail.com> wrote:
I have a question concerning the template classes and their parameters.
Is it possible to set a condition on the template parameters in a way
that could block the compilation if the condition isn't true?
(e.g. by throwing an error message to the compiler).
And is it possible to use this type of condition to make the compiler
choose the best method (between some others).


Here is a proposal from Dr. Stroustrup about introducing a type system
for templates (called "concepts") that may be of interest.

http://www.research.att.com/~bs/popl06.pdf

--
Marcus Kwok
Feb 22 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.