Re: Virtual Functions And Inline Definition
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.
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. 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.
Thanks.