473,804 Members | 3,163 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1672
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_precisi on_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
2631
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" xmlns="http://ltsc.ieee.org/xsd/LOMv1p0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" version="1.0" encoding="ISO-8859-1" indent="yes"/> <!-- rename ims datetime tags to IEEE dateTime tags --> <xsl:template match="ims:datetime">
4
2062
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 () { python -c ' import os, sys, py_compile;
8
2699
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 partially? Something like: fileA.c fileB.c And I can compile fileA.c with debug info and compile fileB.c without debug info?
9
3769
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 IF; END IF; ". Expected tokens may include: "END". LINE NUMBER=1285. SQLSTATE=42601 I tried creating a new tablespace + buffer pool on this database but still
7
1686
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 I touch it. During the beta there used to be something like mywebsite.com/compile.??? that you could run. Does it still exist?
4
8471
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 "Q:\python\python23.zlib\urllib.py", line 78, in urlopen INFO File "Q:\python\python23.zlib\urllib.py", line 159, in open INFO File "Q:\python\python23.zlib\urllib.py", line 957, in splittype INFO AttributeError
4
1390
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? a bug or another reason?
5
1526
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 of any type. luke *.cc, *.h, *hpp Thanks Torben
4
2320
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 not match the application name. This is a bit disconcerting but seems to not affect anything. Any reason why A2003 doesn't know the name of the app it's compiling. I never worried about something like this with A97 since it simply says "Compile...
6
1960
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 non- comment Python statement, there is no error. Here's an example (using 2.5.1 on OS X) .... def foo(): .... print 'bar' """ <code object <moduleat 0x79608, file "", line 2>
0
9707
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10586
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10338
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.