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

O

Ondrej Spanel

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;
}
 
K

Kapil Khosla [MSFT]

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
 
A

Antti Keskinen

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
 
C

Carl Daniel [VC++ MVP]

Ondrej said:
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 said:
printf("atd2 %g\n",atd2(0.5));
getchar();
return 0;
}

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

-cd
 
O

Ondrej Spanel

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top