isConst - help

S

sks_cpp

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 :)
 
T

tom_usenet

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 said:
{
typedef U Result;
enum { isConst = false };
};

struct UnConst<class U* const>

Should be:
template <class U>
{
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top