468,272 Members | 2,202 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,272 developers. It's quick & easy.

condition on template variables

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
3 3115
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
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
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.

Similar topics

9 posts views Thread by Andrea Maschio | last post: by
5 posts views Thread by steven | last post: by
9 posts views Thread by jc | last post: by
5 posts views Thread by mast2as | last post: by
2 posts views Thread by Tony | last post: by
reply views Thread by Orin | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.