472,792 Members | 4,584 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

Similar topics

2
by: Chris | last post by:
I have tableA, defined as: field1 varchar2(10), field2 varchar2(10), field3 varchar2(10) I have host variables defined as: v1 pic x(10) varying v2 pic x(10) varying
9
by: Andrea Maschio | last post by:
Hi, i have a terrible noobie frustration formatting an XML file like this: <Dipendente Id="1" Anno="2003" Nome="pippo" Cognome="pippi" Nato_il="10/03" Email="pippo@emailprovider.it" Esito="ok"/>...
10
by: noleander | last post by:
I've got an application that uses Pthread to do threading. Mostly Im using Condition Variables and the associated function calls: - pthread_cond_wait() - pthread_cond_signal() -...
5
by: steven | last post by:
Hello, I've created a Word-template for a letter. I've made some fields in the template (such as 'customer number', 'name', ...) as follows: insert field - DocVariabele with name 'customer...
9
by: jc | last post by:
Hi all, I have a data type to use that I can't modify its codes. E.g. This template data type is data_type. When I declare a variable of this data_type, I write as following: data_type(8,...
5
by: mast2as | last post by:
Hi guys Here's the class I try to compile (see below). By itself when I have a test.cc file for example that creates an object which is an instance of the class SpectralProfile, it compiles...
2
by: Tony | last post by:
What's all the hullabuloo dissing events in favor of condition variables? Is it just certain uses of events (Win32 events that is) that is seen as bad practice? And can't they be used just like...
0
by: Orin | last post by:
Hi, Here is my task: - need to get text from a template variable. For example we have a text with templates "Cars" and "Producers": "{{Cars|Toyota=blablabla |Moskvitch=blablabla...
9
by: rtalbot | last post by:
I've got a container that looks like this: template <class T> class Foo { public: Foo() : _data(), _status(1) { } Foo(T) : _data(T), _status(0) { } ~Foo() { }
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.