Re: Alternatives to #define?

From: John Harrison (john_andronicus_at_hotmail.com)
Date: 07/02/04


Date: Fri, 2 Jul 2004 13:29:40 +0100


"Carl Ribbegaardh" <carl_ribbegaardh.nospam@hotmail.com> wrote in message
news:2kl21iF3orqnU1@uni-berlin.de...
> What other c++ constructs can I use instead of #define for executing a
> couple of functions?
> Example:
>
> #define DO_STUFF doThis(); doThat();
>
> I'd guess that I can either use a template function, an inlined function
or
> an inlined static method.
>
> //1
> namespace MyUtils
> {
> template<>
> void doStuff()
> {
> doThis();
> doThat();
> }
> }
>
> //2
> namespace MyUtils
> {
> inline void doStuff()
> {
> doThis();
> doThat();
> }
> }
>
> //3
> class MyUtils
> {
> public:
> static inline void doStuff() const
> {
> doThis();
> doThat();
> }
> }
>
> I *believe* that the template version always is inlined, and that the
other
> 2 versions is probably inlined.

No that is not true. I guess you are thinking of the exception that
templates have from the normal one definition rules for functions and
classes. But that's an entirely seperate issue.

> Are theese approaches correct?

They are all correct.

> Which should be preferred?

The second

> Are there any other better way?

What's wrong with the second method? Why is it even an issue?

john



Relevant Pages

  • Re: Templates and Non-Inline Functions
    ... I'm trying to better understand how a template function can be ... prevented from being inline. ... Jonathan Wood ... Available for consulting: http://www.softcircuits.com/jwood/resume.htm ...
    (microsoft.public.vc.mfc)
  • Re: template function --compiler cannot resolve
    ... "Victor Bazarov" wrote: ... > Jazz wrote: ... >> I have a very simple template function declared in the MyInclude.h, ... > declare it 'inline'. ...
    (microsoft.public.vc.language)
  • Re: Alternatives to #define?
    ... >> namespace MyUtils ... >> inline void doStuff() ... >> static inline void doStuff() const ... read lines like "templates are inlined by definition" but I might have ...
    (comp.lang.cpp)