Compile-time checks using tag classes

Discussion in 'C++' started by Leon, Feb 12, 2004.

  1. Leon

    Leon Guest

    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.
     
    Leon, Feb 12, 2004
    #1
    1. Advertising

  2. 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
     
    Yannick Le goc, Feb 12, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shruds
    Replies:
    1
    Views:
    870
    John C. Bollinger
    Jan 27, 2006
  2. Amit
    Replies:
    1
    Views:
    351
    Srini
    May 6, 2005
  3. Nagaraj
    Replies:
    1
    Views:
    888
    Lionel B
    Mar 1, 2007
  4. Carter
    Replies:
    2
    Views:
    517
    Carter
    Mar 4, 2009
  5. loudking
    Replies:
    3
    Views:
    491
    Ben Bacarisse
    Nov 30, 2009
Loading...

Share This Page