C++ Template Question

Hello,

I am trying to create a template that behaves differently
if it is give a primative type vs a class. I should be easy
enough but my brain seems to be stuck in neutral.

Here is the general problem


template< class T, class S>

struct Node
{
Node* MyNode;
T Value;
}


template< class T, class S>

class Myclass
{
T GetValue();
operator== (T rhs);
Node node;
}


In the case that T is a class, T will look somethink like this.

template< class T, class S>

class MyVector
{
int size;
float length;
S value;
}


Here are the requirements.

If T is a a primative (int, long, float, double, char...), S will be a primative of the same type

GetValue will return the value of classInst1.node.value
The operator== will compare classInst1.node.value == classInst2.node.value


If T is a class, S will be a primative.

GetValue will return the value of classInst1.node.value
The operator== will compare classInst1.node.<T>value.value == classInst2.node.<T>value.value


so I could have a program like this

main()
{
Myclass<int, int> Foo1;
Myclass<int, int> Foo2;
Myclass<MyVector, float> Bar1;
Myclass<MyVector, float> Bar2;

int myInt = Foo1.GetValue(); //returns Foo1.node.value
MyVector myVect = Bar1.GetValue(); //returns Bar1.node.value

if ( Foo1 == Foo2 ) {} //compares Foo1.node.value == Foo2.node.value
if ( Bar1 == Bar2 ) {} //compares Bar1.node.value.value == Bar2.node.value.value
}


Thank in advance for any help.

byteherder
Last edited on
Using boost::type_traits::is_pod...

Here is a start. It does not implement the second requirement that if T is a class, S must be a POD. (What if T is an enum?)

(not compiled)

1
2
3
4
5
6
7
8
9
10
#include <boost/type_traits/is_pod.h>

template< typename T, typename S, bool IsTPod = boost::is_pod<T>::value >
struct Node;

template< typename T >
struct Node< T, T, true >
{
    // stuff
};


It's a start, but you have more work to do.

Topic archived. No new replies allowed.