Re: Virtual Functions And Inline Definition
On May 8, 11:49 am, Neelesh <neelesh.bo...@gmail.com> wrote:
On May 8, 1:59 pm, James Kanze <james.ka...@gmail.com> wrote:>
On May 7, 9:04 pm, Neelesh <neelesh.bo...@gmail.com> wrote:
On May 7, 11:36 pm, Marcelo De Brito <Nosopho...@gmail.com> wrote:
Why is not possible to define a pure virtual inline
function in C++?
It is possible (but the 'inline' request will be ignored).
It certainly will not be ignored.
The above inline request can be honored by the compiler only
if the compiler can prove that this function is never called
polymorphically. I guess that is extremely difficult.
First, the standard requires that the "request" be honored. The
way the "one definition rule" works is different for inline
functions and non-inline functions. And this is, strictly
speaking, the only formal difference. (A good compiler will, of
course, take the recommended hint into consideration. Unless it
can do better by ignoring it.) A function declared inline
*must* be defined in every translation unit it uses; a function
which in not declared inline must be defined in exactly one
translation unit, no more, no less. And a compiler which
ignores is broken, to the point of being unusable. (But I've
never seen a compiler which ignored it.)
Second, a function which is declared virtual is always called
polymorphically---i.e. the actual function called will depend on
the dynamic type of the object. How the compiler achieves this
is its business, of course, but typically, all of the compilers
I know use exactly the same mechanism as for a non-inline
function anytime they know the dynamic type. Which isn't
necessarily that rare, depending on how the object is used.
The following code compiles well.
class X
{
virtual void f() = 0;
};
inline void X::f()
{
}
For example:
Yes. And if the compiler actually ignored the inline
request, you couldn't use such a header in more than one
translation unit.
IMHO, even if the compiler actually ignores the inline
request, the very thing that "we have requested the compiler
to make the function inline" gives us all the rights of
multiple (identical) defintions for that function, one per
translation unit.
Yes. That's what the inline declarations requests.
Quoting the standard -
7.1.2/2: A function declaration (8.3.5, 9.3, 11.4) with an
inline specifier declares an inline function. The inline
specifier indicates to the implementation that inline
substitution of the function body at the point of call is to
be preferred to the usual function call mechanism. An
implementation is not required to perform this inline
substitution at the point of call; however, even if this
inline substitution is omitted, the other rules forinline
functions defined by 7.1.2 shall still be respected.
7.1.2/4: An inline function shall be defined in every
translation unit in which it is used and shall have exactly
the same definition in every case
This means that as long as we specify a function as "inline",
we should (rather we must) define it in all translation units.
Hence using such a header in multiple translation units should
not be a problem.
Yes. And. That's basically what I said: if you declare a
function inline, the compiler is required to obey that request,
and allow (or rather require) the definition to be present in
every translation unit where the function is used.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34