Compile-time checks using tag classes

L

Leon

Hi all.

I'm using a thrid-party software library that uses two tag classes (Tag_true
and Tag_false) to flag if certain operations are available in a (template)
class.
Now I think the idea behind them is to be able to perform compile-time
checks, but I don't understand how...

These tag classes are defined as empty structs, i.e.:
struct Tag_true {};
struct Tag_false {};

Now, in a class definition they use the following construction:
class Fixed_precision_nt{
public:
typedef Tag_false Has_gcd;
typedef Tag_true Has_division;
typedef Tag_false Has_sqrt;
//<snip>
};

indicating that this class supports division, but not square root or gcd
calculation. In my application, I want to be able to perform compile-time
tests on these tags, something like this:

#define Fixed_precision_nt scalar
//<snip>
#if scalar::Has_gcd == Tag_true
//...
#endif

What is the proper way of doing this? When I try to compile the above code,
it
complains on the '#if' line with "unexpected tokens following preprocessor
directive - expected a newline". What am I doing wrong?

Thanks,
Leon.
 
Y

Yannick Le goc

Leon said:
Hi all.

I'm using a thrid-party software library that uses two tag classes (Tag_true
and Tag_false) to flag if certain operations are available in a (template)
class.
Now I think the idea behind them is to be able to perform compile-time
checks, but I don't understand how...

These tag classes are defined as empty structs, i.e.:
struct Tag_true {};
struct Tag_false {};

Now, in a class definition they use the following construction:
class Fixed_precision_nt{
public:
typedef Tag_false Has_gcd;
typedef Tag_true Has_division;
typedef Tag_false Has_sqrt;
//<snip>
};

indicating that this class supports division, but not square root or gcd
calculation. In my application, I want to be able to perform compile-time
tests on these tags, something like this:

#define Fixed_precision_nt scalar
//<snip>
#if scalar::Has_gcd == Tag_true
//...
#endif

What is the proper way of doing this?

That is not the way to use templates.
Here it is:
You parameterize your classes by a type that will represent Tag_true or
Tag_false:

// declaration.
template<class GCD> class A;

// specialization for Tag_true and Tag_false:
template<>
class A<Tag_true>
{
//implementation of A when template parameter is Tag_true
};

template<>
class A<Tag_false>
{
//implementation of A when template parameter is Tag_false
};

So you can use it:

A<Fixed_precision_nt::Has_gcd> myA;

and you will have myA that depends at compile-time of
Fixed_precision_nt::Has_gcd.

Yannick
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top