Jump to content
  • Advertisement
Sign in to follow this  
Dragon_Strike

passing template classes as general arguments *solved

This topic is 3985 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

maybe not the best subject name... but here is what i want to achieve... template <class T> class A { public: T value; } A Double(A v1) { return v1+v1; } A<int> intType; A<float> floatType; A intResult = Double(intType); A floatResult = Double(floatType); is it possible to generalize templates like this? [Edited by - Dragon_Strike on January 15, 2008 4:21:37 PM]

Share this post


Link to post
Share on other sites
Advertisement
emplate <class T>
class A
{
public:
typedef T Type;
T value;
}


template < class T >
A<T> Double( A<T> v1 );


typedef A<int> intType;
typedef A<float> floatType;

intType::Type intResult = Double(intType);
floatType::Type floatResult = Double(floatType);

Share this post


Link to post
Share on other sites

template <typename T>
struct A {
T value;
};

template <typename T>
A<T> double(A<T> val) {
A<T> temp = { val.value + val.value };
return temp;
}

This would look less silly if A<> had operator+ defined for it.

Share this post


Link to post
Share on other sites
Quote:
Original post by Dragon_Strike
template <class T>
class A
{
public:
T value;
}


template <class T>
T Double(T v1)
{
return v1+v1;
}


A<int> intType;
A<float> floatType;

A<int> intResult = Double(intType);
A<float> floatResult = Double(floatType);


You can generalize the function but you have to pass the type of the result variables (int or float).

EDIT: Choose the answer you want :D

Share this post


Link to post
Share on other sites
template <typename T>
A<T> double(A<T> val) {
A<T> temp = { val.value + val.value };
return temp;
}

i want to avoid declaring the function as a template since the function will be define in another file... if i write it like that i will get an compiler error telling me that T is undefined...

EDIT::

rly what i want to do is to avoid having to write a function for each type.. like this...

int Double(A<int> v1)
{
return v1+v1;
}

float Double(A<float > v1)
{
return v1+v1;
}

Share this post


Link to post
Share on other sites
template<typename T>
class A
{
public:

T value;
};

template <typename T>
A<T> Double(A<T> val)
{
A<T> temp = { val.value + val.value };
return temp;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Dragon_Strike
u guys are right im just being stupid... i had a mistype that caused a compiler error which made me think it wasnt possible...


Quote:
Original post by SiCrane
A<T> double(A<T> val) {



Not this one by any chance? [wink]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!