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