473,320 Members | 2,071 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.

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 3476
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() { }
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
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...
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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

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.