472,347 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,347 software developers and data experts.

Compiler error - incorrect error message "illegal type for non-type template parameter"

The code below does not compile with .NET 2003, I get folowing error:

w:\c\Pokusy\delegTemplArg\delegTemplArg.cpp(11) : error C2993: 'float' :
illegal type for non-type template parameter 'x'

The error shows compiler is quite confused - x is not a template parameter
at all.

I have found two workarounds, but still I think the code should compile.

Workaround 1: declare member RetType (Type::*member)(float x) const using a
typedef:
typedef RetType (Type::*DelegateFunctionType)(float x) const;
template <class Type, class RetType, DelegateFunctionType>

Workaround 2: ommit the parametr name in the member function prototype:
template <class Type, class RetType, RetType (Type::*member)(float)
const>

Regards
Ondrej
--------------------------------
#include <stdio.h>

class FooA
{
public:
float GetValue1(float x) const {return 1;}
float GetValue2(float x) const {return 2;}
};

/// delegate - member passed via template argument
template <class Type, class RetType, RetType (Type::*member)(float x) const>
class DelegateTF
{
Type &_type;
public:
DelegateTF(Type &type):_type(type){}
RetType operator () (float x) const
{
return (_type.*member)(x);
}
};

int main()
{
FooA a;
DelegateTF<FooA,float,FooA::GetValue2> atd2(a);
printf("atd2 %g\n",atd2(0.5));
getchar();
return 0;
}
Nov 17 '05 #1
4 3898
Thanks Ondrej,
I have verified that this is a bug with our compiler and have logged it in
our database. Hopefully it will be fixed for whidbey .
Your workarounds still work fine with the recent compilers.
Regards,
Kapil

"Ondrej Spanel" <on***********@bistudionospam.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
The code below does not compile with .NET 2003, I get folowing error:

w:\c\Pokusy\delegTemplArg\delegTemplArg.cpp(11) : error C2993: 'float'
: illegal type for non-type template parameter 'x'

The error shows compiler is quite confused - x is not a template parameter
at all.

I have found two workarounds, but still I think the code should compile.

Workaround 1: declare member RetType (Type::*member)(float x) const using
a typedef:
typedef RetType (Type::*DelegateFunctionType)(float x) const;
template <class Type, class RetType, DelegateFunctionType>

Workaround 2: ommit the parametr name in the member function prototype:
template <class Type, class RetType, RetType (Type::*member)(float)
const>

Regards
Ondrej
--------------------------------
#include <stdio.h>

class FooA
{
public:
float GetValue1(float x) const {return 1;}
float GetValue2(float x) const {return 2;}
};

/// delegate - member passed via template argument
template <class Type, class RetType, RetType (Type::*member)(float x)
const>
class DelegateTF
{
Type &_type;
public:
DelegateTF(Type &type):_type(type){}
RetType operator () (float x) const
{
return (_type.*member)(x);
}
};

int main()
{
FooA a;
DelegateTF<FooA,float,FooA::GetValue2> atd2(a);
printf("atd2 %g\n",atd2(0.5));
getchar();
return 0;
}

Nov 17 '05 #2
Hi !

Like Ondrej already stated, it's a bug.

However, the workaround 2 seems to like a "standard legit" way to do this. I
have rarely seen that a pointer-to-function or pointer-to-member would
specify parameter names. For example:

int (ClassA::*ptrFunc)(float, int, int) = &AObj::Func1;

or

int (*ptrFunc)(float, int, int) = &Func1;

Neither has parameter names specified. In light of this, a template
declaration of

template<class A, typename RetVal, RetVal (A::*pFunc)(float,int,int)>

seems valid and correct.

If pFunc above would have parameter names specified, would it change
anything ? It's nice to know that it'll be fixed, although I can't quite
understand why :)

If someone knows if there is a difference, I'd be happy to know.

-Antti Keskinen
"Ondrej Spanel" <on***********@bistudionospam.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
The code below does not compile with .NET 2003, I get folowing error:

w:\c\Pokusy\delegTemplArg\delegTemplArg.cpp(11) : error C2993: 'float'
: illegal type for non-type template parameter 'x'

The error shows compiler is quite confused - x is not a template parameter
at all.

I have found two workarounds, but still I think the code should compile.

Workaround 1: declare member RetType (Type::*member)(float x) const using
a typedef:
typedef RetType (Type::*DelegateFunctionType)(float x) const;
template <class Type, class RetType, DelegateFunctionType>

Workaround 2: ommit the parametr name in the member function prototype:
template <class Type, class RetType, RetType (Type::*member)(float)
const>

Regards
Ondrej
--------------------------------
#include <stdio.h>

class FooA
{
public:
float GetValue1(float x) const {return 1;}
float GetValue2(float x) const {return 2;}
};

/// delegate - member passed via template argument
template <class Type, class RetType, RetType (Type::*member)(float x)
const>
class DelegateTF
{
Type &_type;
public:
DelegateTF(Type &type):_type(type){}
RetType operator () (float x) const
{
return (_type.*member)(x);
}
};

int main()
{
FooA a;
DelegateTF<FooA,float,FooA::GetValue2> atd2(a);
printf("atd2 %g\n",atd2(0.5));
getchar();
return 0;
}

Nov 17 '05 #3
Ondrej Spanel wrote:
The code below does not compile with .NET 2003, I get folowing error:

w:\c\Pokusy\delegTemplArg\delegTemplArg.cpp(11) : error C2993:
'float' : illegal type for non-type template parameter 'x'

The error shows compiler is quite confused - x is not a template
parameter at all.

I have found two workarounds, but still I think the code should
compile.
Yes, it should, as has been noted previously. The formal parameter name
('x') is no different than a parameter name in a function prototype - it
serves no purpose other than documentation but is legal.
int main()
{
FooA a;
DelegateTF<FooA,float,FooA::GetValue2> atd2(a);
This is non-standard and should not compile (VC allows it as an extension
unless you compile with /Za). It should read

DelegateTF<FooA,float,&FooA::GetValue2> atd2(a);
printf("atd2 %g\n",atd2(0.5));
getchar();
return 0;
}


Thanks for posting a complete repro case! So few people do.

-cd
Nov 17 '05 #4
> If pFunc above would have parameter names specified, would it change
anything ? It's nice to know that it'll be fixed, although I can't quite
understand why :)

If someone knows if there is a difference, I'd be happy to know.


There is no difference other than different code clarity. Parameter names
can improve readability of the code.

It is not a serious bug, given there is an easy workaround, but still I am
glad it will be fixed - thanks to MS compiler team. Writing template code is
hard enough in itself, and when you get a cryptic error messages like this
during development, it makes it even harder. I am very glad to see the
quality of template support in VC++ gets much better with each release - it
is already a huge improvement over VC++6.

Regards
Ondrej
Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: mirnazim | last post by:
Hi, There are great Python Web Application Framework. But most of them are meant for content oriented web apps. Is there something that can...
2
by: qe_Cui | last post by:
Hello everyone: I am using vb.net 2003, sqlserver2000 database, and windows 2000 professional OS. In the program, I encrypt the login user's...
4
by: L Mehl | last post by:
Hello -- I am using Enterprise Architect, a UML modeling tool, which is built on a pre-Access 2000 mdb. The database describing the model has...
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this?...
0
by: qe_Cui | last post by:
Hello everyone: I am using vb.net 2003, sqlserver2000 database, and windows 2000 professional OS. In the program, I encrypt the login user's...
4
by: Påhl Melin | last post by:
I have some problems using conversion operators in C++/CLI. In my project I have two ref class:es Signal and SignalMask and I have an conversion...
134
by: jacob navia | last post by:
Hi Suppose you have somewhere #define BOOL int and somewhere else typedef BOOL int;
3
by: Eckhard Schwabe | last post by:
I only found one post on Google where someone mentions the same problem with a DataSet: XmlDataReader in .Net 1.1 can not read XML files from a...
3
by: Shilpa | last post by:
Hi, I have a OpenFileDialog on my windows form whose filter is *.*. I want the users to be able to further filter the files by giving *.doc or...
2
by: muslimak | last post by:
I have a perl script that throws an error "illegal division by zero" chunk 37. FYI, this script was the one that worked fine two days back. There...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.