473,386 Members | 1,754 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,386 software developers and data experts.

Compile-time checks using tag classes

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.
Jul 22 '05 #1
1 1656
Leon wrote:
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

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Jordan Willms | last post by:
My xsl stylesheet is as simple as follows: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:ims="http://www.imsglobal.org/xsd/imsmd_v1p2"...
4
by: Jari Aalto | last post by:
Please suggest comments how can I make this script to work from bash. Also how can I skip better the argument from command line without hte extra variable i? #!/bin/bash function compile ()...
8
by: Davy | last post by:
Hi all, I use VC and gcc/gdb to compile and debug C/C++ files. But I found some of the debug version of the compiled files are too large to be run in a small RAM. Can I compile C/C++ Debug...
9
by: serge | last post by:
I have a stored procedure that is over 1000 + lines of code. When i try to compile I get the following error: ABC.TEST123: 1285: SQL0104N An unexpected token "END" was found following " END...
7
by: Arne | last post by:
I am porting a website to ASP.net 2.0. Temporarily I compile with Visual Studio 2003 and deploying to ASP.net 2.0. How do I compile my website under ASP.Net 2.0? I know it can compile each page as...
4
by: livin | last post by:
my log... INFO urllib.urlopen('http://192.168.1.11/hact/kitchen.asp', urllib.urlencode({'Action': 'hs.ExecX10ByName+Kitchen+Lights%2C+On %2C+100&x=4&y=6'})) INFO INFO File...
4
by: ygao | last post by:
>>compile('U"ÖĞ"','c:/test','single') <code object ? at 00F06B60, file "c:/test", line 1> <code object ? at 00F06BA0, file "c:/test", line 1> u'\xd6\xd0' u'\u4e2d' why is the result different?...
5
by: Torben Laursen | last post by:
Hi Often I just want to compile one c++ file in a project the check it for errors. Now I can right click on a *.cpp file and select "Compile" But is there a way and short cut to compile a file...
4
by: Salad | last post by:
I have a database called MyApp.MDB. When I go into a module and want to compile the thing, it might have, on the menu bar line "Compile MyAppTest". IOW, the app name it's about to compile does...
6
by: Ed Leafe | last post by:
I've noticed an odd behavior with compile() and code that does not contain a trailing newline: if the last line is a comment inside of any block, a syntax error is thrown, but if the last line is a...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.