jochenK

Hello all!

I'm new to C++ templates and need some help.

I want to create a template class like this:

Code Block

template<class T> Testclass

{

private:

T value;

public:

void fromString(System::String^ input)

{

this->value = System::convert::ToT(input); //

}

}

Of course, there is no function ToT(System:Tongue Tiedtring^) in System::Convert.

But if T would be a bool, the function System::Convert::ToBoolean(System:Tongue Tiedtring^).

Same for int and so one.

Now is there a way to tell the compiler which function to use

For example:

typedef Testclass boolTestclass; //The compiler knows which function to choose !

Thanks for your help.

Jochen



Re: Visual C++ General C++ template using System::Convert functions

crescens2k

Maybe the use of C++ RTTI could help you here, but unfortunately templates are not supported that well in .net. You should either look at Generics or find another way of doing what you want.






Re: Visual C++ General C++ template using System::Convert functions

Viorel.

I would suggest considering of "Explicit Specialization of Class Templates" [http://msdn2.microsoft.com/en-us/library/c401y1kb(VS.80).aspx].

For example:

Code Block

template< typename T >

class MyConverter;

template< >

class MyConverter< bool > // specialization for ˇ°boolˇ±

{

public:

static bool fromString(System::String ^ input)

{

return System::Convert::ToBoolean(input);

}

};

template< >

class MyConverter< int > // specialization for ˇ°intˇ±

{

public:

static int fromString(System::String ^ input)

{

return System::Convert::ToInt32(input);

}

};

Your class can be defined like this:

Code Block

template< typename T >

class Testclass

{

T value;

public:

void fromString(System::String ^ input)

{

value = MyConverter< T >::fromString(input);

}

};

Now your class can work with bool and int types:

Code Block

Testclass< int > tc1;

Testclass< bool > tc2;

tc1.fromString("1234");

tc2.fromString("false");

By defining more specialization of MyConverter class, you extend the application area of your class.

I hope this makes sense.





Re: Visual C++ General C++ template using System::Convert functions

jochenK

Thanks Viorel for your help!

But what is the advantage of using a template, then It seems like I have to create a specialized template for every object type. I thought is was the advantage of using a template to do not need to create a new class for every type.

It seems to me that in my special case it is not the best solution to use a template...

Thanks,

Jochen





Re: Visual C++ General C++ template using System::Convert functions

Viorel.

I think the advantage is achieved if the above MyConverter class is already created by someone or by you for a range of data types: you will not need to define a new Testclass class for each type.

In your case you can also consider non-template case: create a set of classes like TestclassBool, TestclassInt etc. based on corresponding System::Convert functions.

But the template version probably has a sense if is to be used inside other template contexts, for example:

template< typename T >

class OtherClass

{

Testclass< T > t;

};