Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Class templates and inlining (OT)

Author: David Rasmussen

Date: 14:38:39 12/23/02

Go up one level in this thread


On December 23, 2002 at 16:20:48, Sune Fischer wrote:

>Hi, I need some help.
>
>In order to simplify my code, I am making a small utility class (or library if
>you will).
>
>I want to do Swap, Max, Sort and all that the smart way, no macros!
>For this I think a template class would be a good idea, with static functions
>cause I don't have any "util" objects, so it's just a namespace container.
>
>So far it's not quite compiling, I get this message in MSVC6.0
>"error C2955: 'CUtil' : use of class template requires template argument list"
>
>what does that mean? I did just as the book said! :) (except my function is
>static)
>

There are several possibilities:
1. MSVC6 is not the most compliant compiler on earth, so don't expect anything
2. Maybe you are using this the wrong way? If you wrap things in a class, you
will have to explicitly give the type argument when you use the static member
functions:

	int c=CUtil<int>::Max(a,b);

Which is maybe not quite what you wanted. But that what you made :)
You might want to wrap the functions in a namespace instead. There are several
different ways of doing what you want to do. The best thing is for you to learn
how on your own. And if you really have to ask somebody, ask in comp.lang.c++,
not CCC :)

>Different question, even more serious.
>How tha' heck do I inline functions in C++?
>

In pure ISO C++, you can't force inlining. You can give the implementation
(compiler, linker etc.) a hint by using the inline keyword. But there are no
guarantees. Some implementations don't inline anything. Some inline a little bit
(if you define member functions in the class declarations, the implementation
will try to inline them), some require that the inlined part is in a header
file, some implementations are so smart that they can inline across compilation
units and they can even inline recursive and mutually recursive functions to
some extent (unrolling) or determine the result of a constant call to a
recursive function at compile time (factorial(6) can be calculated recursively
by a smart implementation at compile time etc.).
What your implementation requires (MSVC6), you'll have to check in the docs.

>I want these Swap and Max to be inlined, I haven't figured out how to do that in
>C++. The only way to inline is to write the whole function inside the header
>file in the class, why is that? Isn't there a way to just have the prototype in
>the class, and have the inlined function written in the cpp file?

In some implementations. In MSVC and gcc, no.

>What good is cpp files then, seems I only ever need headers???
>
>So far I've lost 15% speed in writing from C to C++, the main problem is this
>inlining issue, it's a real speed killer.
>

Inlining hasn't been a practical problem with me, with MSVC. I just put the
relevant parts in a header file.

C++ isn't slower for chess programming than C if used right. C++ can easily be
faster than C. The most important benefit though, is the type safety and the
superior designs possible.

/David



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.