S
skscpp
I copied this from several sources online. Why doesn't it work?
// ================================================================
#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 };
};
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<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;
}
// ================================================================
Here is the output:
It's NOT a const
Hooray - It's NOT a const
// ================================================================
#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 };
};
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<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;
}
// ================================================================
Here is the output:
It's NOT a const
Hooray - It's NOT a const