Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > isConst - help

Reply
Thread Tools

isConst - help

 
 
sks_cpp
Guest
Posts: n/a
 
      07-24-2003
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


 
Reply With Quote
 
 
 
 
tom_usenet
Guest
Posts: n/a
 
      07-24-2003
On Thu, 24 Jul 2003 12:06:20 GMT, "sks_cpp" <(E-Mail Removed)>
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Help Help Pentax S5i Help needed (Please) The Martian Digital Photography 14 06-20-2008 07:56 AM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 1 07-16-2004 01:12 PM
HELP WANTED HELP WANTED HELP WANTED Harvey ASP .Net 0 07-16-2004 10:00 AM
HELP! HELP! HELP! Opening Web Application Project Error =?Utf-8?B?dHJlbGxvdzQyMg==?= ASP .Net 0 02-20-2004 05:16 PM
isConst - Loki skscpp C++ 4 07-25-2003 02:56 PM



Advertisments