Templates in VC++ 7.1 Compiler error?

This topic is 5060 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Please look at Enigma's post below - it explains the problem. ---- I've got a strange problem in function template with default parameter value.
template<typename R, R (*convert_fun)(lua_State*, int), int lua_type_id>
R get_val(lua_State* L, int stackindex) // <-- (1)
{ return R(); }

template<typename R>
R get_val(lua_State* L, int stackindex = -1) // <-- (2)
{ return R(); }

template<>
lua_Number get_val<lua_Number>(lua_State* L, int stackindex)
{
return get_val<lua_Number, lua_tonumber, LUA_TNUMBER>(L,stackindex); //error!
//error C2383: 'convert_fun' : default-arguments are not allowed on this symbol
}

//where:
lua_Number lua_tonumber (lua_State *L, int idx);


If I remove the "= -1" in (2) everything is ok (even if I add "= -1" in (1)). I'm really clueless. Any thoughts? [Edited by - Zbyl on January 6, 2005 12:49:55 PM]

Share on other sites
Well, the error simply means that you can't have default values for pointers to functions. Since these are evaluated at compile time, a default value for (2) propagates down to a default value for (1) in your specialization, which the compiler catches and complains at. Meanwhile, if (2) doesn't have a default value, then regardless of the default value of (1) the value of (2) must always be passed and will always propagate down to (1) in your specialization. Thus the compiler is fine.

At least that's what I think.

Share on other sites
Zipster: I see what you're saying, but I don't see how the symbol convert_fun depends on the default argument.

Both BCC and MinGW GCC compile the code without error. I've brought it down to what seems to be a minimal test case under Visual C++:
void func0(int){}template <void (*problem_symbol)(int)>void func(int i){}template <typename T>void func2(int i = -1){}template<>void func2<int>(int i){	return func<func0>(i);}

As you can see, I've separated the function names and yet the error remains. It can however be fixed by changing the prototype of the function-pointer template parameter:
void func0(float){}template <void (*problem_symbol)(float)>void func(int i){}template <typename T>void func2(int i = -1){}template<>void func2<int>(int i){	return func<func0>(i);}

indicating to me as if the compiler may be getting confused between functions.

So it may be a bug in Visual C++, but I'm nowhere near confident enough in my knowledge of templates to say with any certainly, so I'll just wait for snk_kid, Polymorphic OOP, petewood or another of the usual suspects to step in and explain it all as if it should be obvious [wink].

Enigma

Share on other sites
I've never even seen a function pointer declaration in a template parameter list before, so I'm more lost than anyone!

Share on other sites
Quote:
 Original post by Zipster...a default value for (2) propagates down to a default value for (1) in your specialization, which the compiler catches and complains at.

But the (2) is not a specialization of (1) - it's a different template (as far as I know...).
And as Enigma said: I don't see how the symbol convert_fun depends on the default argument.
Also in Enigma's code it really look's like a compiler error...
(btw: I use VC++ 7.1)

Share on other sites
I might be guesing here but in this function:
template <void (*problem_symbol)(int)>void func(int i){}
and this function:
template <typename T>void func2(int i = -1){}
problem_symbol and T are template parameters, but they are not used in the functions defined after them. Isn't that a no-no?

If that's not it then here's another thing that looks very wrong:
	return func<func0>(i);
func0 is NOT a type, it is a function pointer!
Maybe you need to do a typedef for something like func0 and specialise in that instead? Or perhaps the function pointer should actually be passed to a parameter of the other function.

Share on other sites
Quote:
 Original post by iMalcI might be guesing here but in this function (...) problem_symbol and T are template parameters, but they are not used in the functions defined after them. Isn't that a no-no?

No - it's all right AFAIK.
Quote:
 Original post by iMalcIf that's not it then here's another thing that looks very wrong (...) func0 is NOT a type, it is a function pointer!

Everything is fine with it - template parameters don't need to be types, they can be values of built-in types. (There is other thing wrong with this function: it returns an 'int', while it should return 'void' - but that's not the source of my problem either.)

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 9
• 9
• 9
• 14
• 12
• Forum Statistics

• Total Topics
633302
• Total Posts
3011274
• Who's Online (See full list)

There are no registered users currently online

×