开发者

Problem understanding this use of template for function

I have used templates for types of parameters, but I came across this thing and I'm not really sure I understand what's going on or have ever read about it.

template<char *name, int iteration, bool high>
void test( short step )
{
   if(high)
     iteration++;
   开发者_如何转开发change(name,iteration);
}

template void test<"John", 5, true>( short );

EDIT : if(bool) was clearly a mistake replaced it by the right thing.


Nothing is going on. The template is just being declared and then explicitly instantiated. It's not being called.

In fact, I doubt it compiles. I don't think if (bool) is a valid expression.

Another reason it doesn't compile is that a string literal cannot be a template parameter. I really wish it could be because it would be quite useful in a lot of metaprogramming constructs I've used. In order to pass a char* to a template you have to do this:

extern char * param = "whatever";
expects_char_param<param>();

And even if this function DID work, and compiled, and bool was replaced with high in the body, it would be an incredibly stupid way of doing it. To figure out WHAT it does though, just pretend the parameters are variables, like they should be, and walk through the code. If you still don't understand... dunno what to tell you.


There is no way in hell that this code will compile. First, you can't pass string literals via template. Second, if (bool)?

The reason you don't understand it is because it's uncompilable nonsense.


That template is completely broken. For one thing, as Noah Roberts has already mentioned, if(bool) won't even compile. For another, iteration is a template parameter, meaning it must be a compile-time constant, so iteration++; won't compile either.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜