I'm having an argument with a cow orker about whether or not it's legal to put the inline keyword in the declaration of a global or member function.
For example, is:
inline void foo(int i);
inline void foo(int i) { write_stuff(i);
}
legal? Is it recommended?
How about:
class A { public: inline void foo(int i);
};
inline void A::foo(int i) { write_stuff(i);
}
Is that legal? Is it recommended?
One of the reasons this comes up is that for member variables and functions, the 'static', and 'virtual' keywords are ONLY legal in the declaration, and not in the definition.
Another question has to do with optimization. If you have a case:
class A { public: inline int getComputedValue() const; inline int getValue() const;
};
inline int A::getComputedValue() const { return(getValue() * 3);
}
inline int A::getValue() const { return(2};
}
If the 'inline' isn't present in the declaration of [A::getValue() const], how does the compiler know to inline it in the definition of [A::getComputedValue() const] without excessive lookahead?
If it wouldn't be too difficult, mailed (as well as posted) responses would be appreciated.
Thanks, --Eric Hopper
[ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ] [ about comp.lang.c++.moderated. First time posters: do this! ]
On 22 Aug 1998 10:19:52 -0400, Eric Hopper <ehop...@globalmt.com> wrote:
>I'm having an argument with a cow orker about whether or not >it's legal to put the inline keyword in the declaration of a global or >member function.
It's legal. You can put inline either in the in-class declaration, in the in-class definition, in the outof-class definition, or in both the in-class declaration and outof-class definition.
If I define an inline func outiside the class, I like to put the 'inline' in both places. This tells readers of the class -- 'hey, this is an inline func, and if it is inlined it should have no func call overhead, but it will look kind of messy if I define it here'.
You can use 'inline' in any function declaration. You can even use 'inline' in friend declarations
-- ---------------------------------- Siemel B. Naran (sbna...@uiuc.edu) ----------------------------------
[ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ] [ about comp.lang.c++.moderated. First time posters: do this! ]
>I'm having an argument with a cow orker about whether or not
Interesting typo.
>it's legal to put the inline keyword in the declaration of a global or >member function.
The inline qualifier is allowed in declarations. To the best of my knowledge, it's also often recommended. As always, whenever you pass more information to the compiler, it is able to do more for you. Consider:
class A { public: f(); g() { return f(); }
};
inline A::f() { return 0;
}
In this case certain compilers won't be able to inline g() correctly. They will generate an out-of-line call in g() for f(). When the compiler discovers that f() is inline, it's too late, the romance is over...
So you're right in the last part of your message. However, this is not generally true, as the compilers may have different degrees of intelligence built in. But if you really would die for inlining, it's better to put the darned inline in there.
Regards,
Andrei
[ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ] [ about comp.lang.c++.moderated. First time posters: do this! ]
Eric Hopper <ehop...@globalmt.com> writes: >I'm having an argument with a cow orker about whether or not >it's legal to put the inline keyword in the declaration of a global or >member function.
It is always legal, except at block scope. There is little point in doing so. See the answer to the next question.
>Another question has to do with optimization. If you have a case: >class A { > public: > inline int getComputedValue() const; > inline int getValue() const; >}; >inline int A::getComputedValue() const >{ > return(getValue() * 3); >} >inline int A::getValue() const >{ > return(2}; >} > If the 'inline' isn't present in the declaration of >[A::getValue() const], how does the compiler know to inline it in the >definition of [A::getComputedValue() const] without excessive >lookahead?
You can't expect a function call to be generated inline if the definition hasn't yet been seen. Move the definition of getvalue ahead of the definition of getComputedValue.
> >I'm having an argument with a cow orker about whether or not > >it's legal to put the inline keyword in the declaration of a global or > >member function.
> It is always legal, except at block scope. There is little > point in doing so. See the answer to the next question.
> >Another question has to do with optimization. If you have a case:
> >class A { > > public: > > inline int getComputedValue() const; > > inline int getValue() const; > >};
> > If the 'inline' isn't present in the declaration of > >[A::getValue() const], how does the compiler know to inline it in the > >definition of [A::getComputedValue() const] without excessive > >lookahead?
> You can't expect a function call to be generated inline if the > definition hasn't yet been seen. Move the definition of getvalue > ahead of the definition of getComputedValue.
I would not be very happy with a compiler which didnt inline due to declaration order. Inlining aside, how do you do any kind of interprocedural optimization without reading in the whole translation unit? (Better still the entire program).
Mark Williams
-----== Posted via Deja News, The Leader in Internet Discussion ==----- http://www.dejanews.com/rg_mkgrp.xp Create Your Own Free Member Forum
[ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ] [ about comp.lang.c++.moderated. First time posters: do this! ]
> I'm having an argument with a cow orker about whether or not > it's legal to put the inline keyword in the declaration of a global or > member function.
It's legal to place 'inline' before either the definition or the declaration or both. With regard to linkage, here's the effect that the placement of the keyword has(*):
Paul Grealish wrote in message <35E15D08.1...@uk.geopak-tms.com>... >With regard to linkage, here's the effect >that the placement of the [inline] has(*): > [...] >(*) Using MSVC++5.0 although I'd expect other > compilers to behave the same way.
According to the standard, inline has no effect on linkage. At one time (ARM) it did make a difference, and compilers are not all up to date in this regard. If you are working with many compilers (or any older compilers), then I'd recommend you be explicit about the linkage you desire (extern or static/unnamed-namespace).
[ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ] [ about comp.lang.c++.moderated. First time posters: do this! ]
On 24 Aug 1998 19:14:42 -0400, Bill Wade <bill.w...@stoner.com> wrote:
>regard. If you are working with many compilers (or any older >compilers), >then I'd recommend you be explicit about the linkage you desire (extern >or >static/unnamed-namespace).
No, 'static' for forcing internal linkage has been deprecated. Use only unnamed namespaces. We had a thread about this recently.
-- ---------------------------------- Siemel B. Naran (sbna...@uiuc.edu) ----------------------------------
[ Send an empty e-mail to c++-h...@netlab.cs.rpi.edu for info ] [ about comp.lang.c++.moderated. First time posters: do this! ]
Paul Grealish <paul.greal...@uk.geopak-tms.com> writes: >It's legal to place 'inline' before either >the definition or the declaration or both. >With regard to linkage, here's the effect >that the placement of the keyword has(*):
The C++ standard says that "inline" have no effect on function linkage. (The original rule was that "inline" made the function "static" unless it was a member function.)