isConst - help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sks_cpp

    isConst - help

    How can I get the following to work? Are the templates defined incorrectly?
    I am running this on Visual C++ 6.0.


    // =============== =============== =============== ==

    #include <iostream>
    using namespace std;

    namespace utl
    {
    template <typename T>
    class TypeTraits
    {
    private:
    template<class U>
    struct UnConst
    {
    typedef U Result;
    enum { isConst = false };
    };

    template<class U>
    struct UnConst<const U>
    {
    typedef U Result;
    enum { isConst = true };
    };

    struct UnConst<class U*>
    {
    typedef U Result;
    enum { isConst = false };
    };

    struct UnConst<class U* const>
    {
    typedef U Result;
    enum { isConst = true };
    };


    struct UnConst<class U const *>
    {
    typedef U Result;
    enum { isConst = true };
    };

    public:

    enum { isConst = UnConst<T>::isC onst };
    };
    }

    class Object
    {
    public:
    Object(int a) { i = a; }
    int i;
    };

    int main()
    {
    Object test(10);
    const Object* t = &test;

    if ( utl::TypeTraits <Object* const>::isConst )
    cout << "Hooray - it's a const :)\n";
    else if ( utl::TypeTraits <Object const *>::isConst )
    cout << "Hooray - it's a const :)\n";
    else if ( utl::TypeTraits <const Object *>::isConst )
    cout << "Hooray - it's a const :)\n";
    else if ( utl::TypeTraits <Object const>::isConst )
    cout << "Hooray - it's a const :)\n";
    else if ( utl::TypeTraits <const Object>::isCons t )
    cout << "Hooray - it's a const :)\n";
    else
    cout << "It's NOT a const :(\n";


    if ( utl::TypeTraits <Object*>::isCo nst )
    cout << "it's a const :(\n";
    else
    cout << "Hooray - It's NOT a const :)\n";

    cout << endl;

    return 0;
    }
    // =============== =============== ===============

    // Output

    It's NOT a const :(
    Hooray - It's NOT a const :)


  • tom_usenet

    #2
    Re: isConst - help

    On Thu, 24 Jul 2003 12:06:20 GMT, "sks_cpp" <sksjava@hotmai l.com>
    wrote:
    [color=blue]
    >How can I get the following to work? Are the templates defined incorrectly?
    >I am running this on Visual C++ 6.0.[/color]

    Visual C++ 6 doesn't have partial template specialization, so you are
    out of luck. You need to upgrade to VC++ 7.1
    [color=blue]
    >
    >// =============== =============== =============== ==
    >
    >#include <iostream>
    >using namespace std;
    >
    >namespace utl
    >{
    > template <typename T>
    > class TypeTraits
    > {
    > private:
    > template<class U>
    > struct UnConst
    > {
    > typedef U Result;
    > enum { isConst = false };
    > };
    >
    > template<class U>
    > struct UnConst<const U>
    > {
    > typedef U Result;
    > enum { isConst = true };
    > };[/color]

    Ok so far. But I'm not sure why you have the following (incorrectly
    written) specializations . They aren't needed.
    [color=blue]
    >
    > struct UnConst<class U*>[/color]

    Should be:

    template <class U>
    struct UnConst<U*>
    [color=blue]
    > {
    > typedef U Result;
    > enum { isConst = false };
    > };
    >
    > struct UnConst<class U* const>[/color]

    Should be:
    template <class U>
    struct UnConst<U* const>

    [color=blue]
    > {
    > typedef U Result;
    > enum { isConst = true };
    > };
    >
    >
    > struct UnConst<class U const *>[/color]

    Again.
    [color=blue]
    > {
    > typedef U Result;
    > enum { isConst = true };
    > };
    >
    > public:
    >
    > enum { isConst = UnConst<T>::isC onst };
    > };
    >}
    >
    >class Object
    >{
    >public:
    > Object(int a) { i = a; }
    > int i;
    >};
    >
    >int main()
    >{
    > Object test(10);
    > const Object* t = &test;
    >
    > if ( utl::TypeTraits <Object* const>::isConst )
    > cout << "Hooray - it's a const :)\n";
    > else if ( utl::TypeTraits <Object const *>::isConst )[/color]

    That isn't a const, by my reckoning.
    [color=blue]
    > cout << "Hooray - it's a const :)\n";
    > else if ( utl::TypeTraits <const Object *>::isConst )
    > cout << "Hooray - it's a const :)\n";
    > else if ( utl::TypeTraits <Object const>::isConst )
    > cout << "Hooray - it's a const :)\n";
    > else if ( utl::TypeTraits <const Object>::isCons t )
    > cout << "Hooray - it's a const :)\n";
    > else
    > cout << "It's NOT a const :(\n";
    >
    >
    > if ( utl::TypeTraits <Object*>::isCo nst )
    > cout << "it's a const :(\n";
    > else
    > cout << "Hooray - It's NOT a const :)\n";
    >
    > cout << endl;
    >
    > return 0;
    >}
    >// =============== =============== ===============
    >
    >// Output
    >
    >It's NOT a const :(
    >Hooray - It's NOT a const :)[/color]

    Take a look at boost's type traits. www.boost.org. They don't work
    (properly) on VC6 though - it is 5 years old now so it isn't
    surprising!

    Tom

    Comment

    Working...