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++-...@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. 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 (sbn...@uiuc.edu)
----------------------------------
>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
>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.
--
Steve Clamage, stephen...@sun.com
In article <6rnfq5$lt5$1...@engnews1.eng.sun.com>,
cla...@Eng.Sun.COM (Steve Clamage) wrote:
> Eric Hopper <eho...@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 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
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(*):
Foo.h Foo.cpp Linkage
----- ------- -------
inline specified 0 0 External
0 1 Internal
1 0 Internal
1 1 Internal
(*) Using MSVC++5.0 although I'd expect other
compilers to behave the same way.
--
+---------------------------------+
| Paul Grealish |
| GEOPAK-TMS Limited |
| Cambridge, England |
| paul.g...@uk.geopak-tms.com |
+---------------------------------+
> [...]
>(*) 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).
>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 (sbn...@uiuc.edu)
----------------------------------
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
>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.)
--
Steve Clamage, stephen...@sun.com
That supposes your compiler supports namespaces. None of the
compilers I use support them, despite their obvious usefulness.
Have fun (if at all possible),
--Eric Hopper