PDA

View Full Version : call static method of template type


Jens Thiel
03-24-04, 08:19 AM
something
Hi,

is there a workaround for T::Parse (other than Convert::ChangeType) in the
following simplified template:

template<class T>
T GetConfig(String* key, T defaultValue)
{
String* value = settings->get_Item(key) ;
if( value )
return T::Parse( value );
return defaultValue;
}

the compiler (7.1) says:

error C2825: 'T::Parse': cannot form a qualified name

Thanks,

Jens.

--
http://ManagedXLL.net/ | http://jens-thiel.de/ | http://QuantLib.net/
Replace MSDN with my first name when replying to my email address!

Hendrik Schober
03-24-04, 09:20 AM
something
Jens Thiel <MSDN@Thiel.de> wrote:
> Hi,
>
> is there a workaround for T::Parse (other than Convert::ChangeType) in the
> following simplified template:
> [...]

This compiles fine for me:

#include <string>

class X {
public:
static X Parse(const std::string&) {return X();}
};

template<class T>
T GetConfig(const std::string* key, T defaultValue)
{
if( key )
return T::Parse(*key);
else
return defaultValue;
}

int main()
{
X x;
std::string s("key");
GetConfig(&s,x);
return 0;
}

> Jens.


Schobi

--
SpamTrap@gmx.de is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers

Jens Thiel
03-24-04, 09:20 AM
something
"Hendrik Schober" <SpamTrap@gmx.de> wrote
> This compiles fine for me:

Well, me /clr...

--
http://ManagedXLL.net/ | http://jens-thiel.de/ | http://QuantLib.net/
Replace MSDN with my first name when replying to my email address!

Gary Chang
03-24-04, 11:20 PM
something
Hi Jens,

> Well, me /clr...

Do you mean your program is .NET managed code and the compiler give you a
error C2825?

If so, would you please give us a more complete program which includes the
class definition?


Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

Jens Thiel
03-25-04, 06:19 AM
something
"Gary Chang" <v-garych@online.microsoft.com> wrote
> Hi Jens,
>
>
> Do you mean your program is .NET managed code and the compiler give you a
> error C2825?
>
> If so, would you please give us a more complete program which includes the
> class definition?
>

Hi Gray, below is small test case. By the way, writing "T.Parse" in the
template gives a nice C1001.

Jens.


using namespace System;
template<class T>
T TryParse(String* value, T defaultValue)
{
try
{
if( value )
return T::Parse( value );
}
catch(Exception*)
{
// ignore
}
return defaultValue;
}

namespace TryParseTest
{
public __gc class Class1
{
public:
void ParseSomeStrings()
{
double d1 = TryParse(S"42.0", 13.0);
Double d2 = TryParse<Double>(S"42.0", 13.0);
Double d3 = TryParse<System::Double>(S"42.0", 13.0);
// also tried int and bool
}
};
}


--
http://ManagedXLL.net/ | http://jens-thiel.de/ | http://QuantLib.net/
Replace MSDN with my first name when replying to my email address!

Gary Chang
03-26-04, 04:20 AM
something
Hi Jens,

Thanks for your sample code!

I ran your code with a wizard generated .NET Console program, it had the
error C2825 during compiling which the same as you figured in your first
message.

When I removed the keyword "__gc", that error would go away. So it appeared
that the managed class has some limitation on the C++ template syntax.



Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

vBulletin v3.6.2, Copyright ©2000-2010, Jelsoft Enterprises Ltd.