isConst - help

Discussion in 'C++' started by sks_cpp, Jul 24, 2003.

  1. sks_cpp

    sks_cpp Guest

    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>::isConst };
    };
    }

    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>::isConst )
    cout << "Hooray - it's a const :)\n";
    else
    cout << "It's NOT a const :(\n";


    if ( utl::TypeTraits<Object*>::isConst )
    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 :)
     
    sks_cpp, Jul 24, 2003
    #1
    1. Advertising

  2. sks_cpp

    tom_usenet Guest

    On Thu, 24 Jul 2003 12:06:20 GMT, "sks_cpp" <>
    wrote:

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


    Visual C++ 6 doesn't have partial template specialization, so you are
    out of luck. You need to upgrade to VC++ 7.1

    >
    >// ===============================================
    >
    >#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 };
    > };


    Ok so far. But I'm not sure why you have the following (incorrectly
    written) specializations. They aren't needed.

    >
    > struct UnConst<class U*>


    Should be:

    template <class U>
    struct UnConst<U*>

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


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


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


    Again.

    > {
    > typedef U Result;
    > enum { isConst = true };
    > };
    >
    > public:
    >
    > enum { isConst = UnConst<T>::isConst };
    > };
    >}
    >
    >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 )


    That isn't a const, by my reckoning.

    > 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>::isConst )
    > cout << "Hooray - it's a const :)\n";
    > else
    > cout << "It's NOT a const :(\n";
    >
    >
    > if ( utl::TypeTraits<Object*>::isConst )
    > 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 :)


    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
     
    tom_usenet, Jul 24, 2003
    #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. =?Utf-8?B?dHJlbGxvdzQyMg==?=

    HELP! HELP! HELP! Opening Web Application Project Error

    =?Utf-8?B?dHJlbGxvdzQyMg==?=, Feb 20, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    812
    =?Utf-8?B?dHJlbGxvdzQyMg==?=
    Feb 20, 2004
  2. Harvey
    Replies:
    0
    Views:
    789
    Harvey
    Jul 16, 2004
  3. Harvey
    Replies:
    1
    Views:
    896
    Daniel
    Jul 16, 2004
  4. =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?=

    HELP - HELP - HELP

    =?Utf-8?B?S2ltb24gSWZhbnRpZGlz?=, Mar 9, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    723
    Eliyahu Goldin
    Mar 9, 2006
  5. skscpp

    isConst - Loki

    skscpp, Jul 23, 2003, in forum: C++
    Replies:
    4
    Views:
    509
    Shane Beasley
    Jul 25, 2003
Loading...

Share This Page