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
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
else if ( utl::TypeTraits<Object const *>::isConst )
cout << "Hooray - it's a const
else if ( utl::TypeTraits<const Object *>::isConst )
cout << "Hooray - it's a const
else if ( utl::TypeTraits<Object const>::isConst )
cout << "Hooray - it's a const
else if ( utl::TypeTraits<const Object>::isConst )
cout << "Hooray - it's a const
else
cout << "It's NOT a const
if ( utl::TypeTraits<Object*>::isConst )
cout << "it's a const
else
cout << "Hooray - It's NOT a const
cout << endl;
return 0;
}
// =============================================
// Output
It's NOT a const
Hooray - It's NOT a const