call static method of template type

  • Thread starter Thread starter Jens Thiel
  • Start date Start date
J

Jens Thiel

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.
 
Jens Thiel said:
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;
}


Schobi

--
(e-mail address removed) is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers
 
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.
--------------------
 
Gary Chang said:
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
}
};
}
 
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.
--------------------
 
Gary Chang said:
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.

So your suggestion for a workaround is: Do not use a managed class!?

At least it is different from the usual: "This has been fixed in Whidbey"
answer...

Replace the "T::Parse" in the former example by "T.Parse" and you get a
C1001. And here is another one from the same day I encountered shortly after
finding out about "virtual bool" the hard way:

namespace ArrayTwoD
{
public __gc class Class1
{
public:
void TestCase(System::Xml::XmlNode* node)
{
String* one[] = new String*[1];
String* two[,] = new String*[1,1];
one[0] = node->Value; // OK
two[0,0] = node->Value; // C1001: INTERNAL COMPILER ERROR
two[0,0] = node->get_Value(); // OK
}
};
}

All these /clr bugs and the unavailability of any service packs make me
recommend to *not* use managed C++ to my customers, and it surely stops C++
programmers in professional environments to make the transition to a managed
environment.


Jens.
 
Hello Jens,

Thanks for your reply. I checked this issue, and found a bug regarding the
C2825 which was fixed in Whidbey Beta 1. Concerning the C1001 compiler
error, you can use the following code to workaround the problem:

two[0,0] = node->Value->ToString();

Please feel free to let me know if you have any problems or concerns.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
Back
Top