Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Virtual Functions And Inline Definition

Reply
Thread Tools

Virtual Functions And Inline Definition

 
 
Marcelo De Brito
Guest
Posts: n/a
 
      05-07-2009
Hi!

Why is not possible to define a pure virtual inline function in C++?

For example:

class c1 {
virtual void f() = 0 {} // ERROR
virtual void g() = 0; // FINE
};

Why?

I appreciate your comments, suggestions, and etc.

Thank you!

Marcelo de Brito
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      05-07-2009
Marcelo De Brito wrote:
> Why is not possible to define a pure virtual inline function in C++?
>
> For example:
>
> class c1 {
> virtual void f() = 0 {} // ERROR
> virtual void g() = 0; // FINE
> };
>
> Why?


Because the Standard says so (9.2, 10.4/2).

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      05-07-2009
Marcelo De Brito wrote:
> Hi!
>
> Why is not possible to define a pure virtual inline function in C++?
>
> For example:
>
> class c1 {
> virtual void f() = 0 {} // ERROR
> virtual void g() = 0; // FINE
> };
>
> Why?
>
> I appreciate your comments, suggestions, and etc.


Because a virtual function is secretly a pointer to a function, and {} inside a
class is the same as an explicit inline. 'inline' is a hint to the compiler it
can push a method's opcodes into its call sites, without the overhead of calling
a function and jumping into a different stack frame. Methods defined inside
classes are 'inline' by default.

Taking the address of an inline function, in turn, blows away that "hint", and
forces the function to appear out-of-line. And the compiler must take the
address to generate the secret pointer for virtual dispatch.

All these rules were invented when C was young. Pure virtual is a late addition,
so it does what other abuses of inline cannot - it puts up an error if you
attempt to mix the virtual and inline concepts together.

The C++ FAQ will have an answer here too, potentially more accurate!

--
Phlip
 
Reply With Quote
 
Neelesh
Guest
Posts: n/a
 
      05-07-2009
On May 7, 11:36*pm, Marcelo De Brito <(E-Mail Removed)> wrote:
> Hi!
>
> Why is not possible to define a pure virtual inline function in C++?
>


It is possible (but the 'inline' request will be ignored). The
following code compiles well.

class X
{
virtual void f() = 0;
};

inline void X::f()
{
}

> For example:
>
> class c1 {
> * virtual void f() = 0 {} // ERROR
> * virtual void g() = 0; * *// FINE
>
> };
>


So what you meant was: Why is it not possible to define a pure virtual
function inside the class body (or, in other words - why is it not
possible to give a function declaraction for a pure virtual function
using "= 0" syntax and also provide a definition there).

The answer to that question is : "a function declaration cannot
provide both a pure-specifier
and a definition" (10.4p2). By "pure-specifier" the c++ standard means
the syntax "= 0".
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      05-07-2009
Phlip wrote:
> Marcelo De Brito wrote:
>> Hi!
>>
>> Why is not possible to define a pure virtual inline function in C++?
>>
>> For example:
>>
>> class c1 {
>> virtual void f() = 0 {} // ERROR
>> virtual void g() = 0; // FINE
>> };
>>
>> Why?
>>
>> I appreciate your comments, suggestions, and etc.

>
> Because a virtual function is secretly a pointer to a function, and {}
> inside a class is the same as an explicit inline. 'inline' is a hint to
> the compiler it can push a method's opcodes into its call sites, without
> the overhead of calling a function and jumping into a different stack
> frame. Methods defined inside classes are 'inline' by default.


So? A regular function can be both virtual and inline (defined in the
class definition).

> Taking the address of an inline function, in turn, blows away that
> "hint", and forces the function to appear out-of-line. And the compiler
> must take the address to generate the secret pointer for virtual dispatch.


That shouldn't prevent the compiler from being able to substitute the
body of the function where it can.

> All these rules


Which ones? That a virtual function is secretly a pointer to a function?

> were invented when C was young. Pure virtual is a late
> addition, so it does what other abuses of inline cannot - it puts up an
> error if you attempt to mix the virtual and inline concepts together.
>
> The C++ FAQ will have an answer here too, potentially more accurate!


Is that a promise?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-08-2009
On May 7, 8:47 pm, Phlip <(E-Mail Removed)> wrote:
> Marcelo De Brito wrote:


> > Why is not possible to define a pure virtual inline function
> > in C++?


> > For example:


> > class c1 {
> > virtual void f() = 0 {} // ERROR
> > virtual void g() = 0; // FINE
> > };


> > Why?


> > I appreciate your comments, suggestions, and etc.


> Because a virtual function is secretly a pointer to a
> function,


Since when?

> and {} inside a class is the same as an explicit
> inline. 'inline' is a hint to the compiler it can push a
> method's opcodes into its call sites, without the overhead of
> calling a function and jumping into a different stack frame.
> Methods defined inside classes are 'inline' by default.


> Taking the address of an inline function, in turn, blows away
> that "hint", and forces the function to appear out-of-line.


Since when? Taking the address of a function does require that
there be a single, out of line instance, but that's true of any
inline function.

> And the compiler must take the address to generate the secret
> pointer for virtual dispatch.


Perhaps. But what does that have to do with anything. That's
an implementation detail---the compiler's problem, not yours.

> All these rules were invented when C was young.


What rules? When C was young (and even today), it didn't have
member functions, much less virtual functions. And when C was
young (until 1999, in fact), it didn't have inline functions.

> Pure virtual is a late addition,


Well, it doesn't seem to be in the first edition of TC++PL. But
it's present in the ARM, and certainly predates templates and
exceptions.

> so it does what other abuses of inline cannot - it puts up an
> error if you attempt to mix the virtual and inline concepts
> together.


Have you actually tried. Inline and virtual are orthogonal;
there's absolutely no problem making a virtual function inline
(and at least one idiom depends on it).

The only reason his first example is illegal is because the
grammar doesn't allow it. Maybe because someone had realized
that the whole concept of defining a function in the class was
wrong, and decided not to support it for anything new. But more
likely simply because no one thought to extend the grammar to
support it, and afterwards, it wasn't considered important
enough to bother with.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      05-08-2009
On May 7, 9:04 pm, Neelesh <(E-Mail Removed)> wrote:
> On May 7, 11:36 pm, Marcelo De Brito <(E-Mail Removed)> 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 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.

--
James Kanze (GABI Software) email:(E-Mail Removed)
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
 
Reply With Quote
 
Neelesh
Guest
Posts: n/a
 
      05-08-2009
On May 8, 1:59*pm, James Kanze <(E-Mail Removed)> wrote:
> On May 7, 9:04 pm, Neelesh <(E-Mail Removed)> wrote:
>
> > On May 7, 11:36 pm, Marcelo De Brito <(E-Mail Removed)> 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.
 
Reply With Quote
 
Alf P. Steinbach
Guest
Posts: n/a
 
      05-08-2009
* Neelesh:
> On May 8, 1:59 pm, James Kanze <(E-Mail Removed)> wrote:
>> On May 7, 9:04 pm, Neelesh <(E-Mail Removed)> wrote:
>>
>>> On May 7, 11:36 pm, Marcelo De Brito <(E-Mail Removed)> 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.


I'm sorry, the above is meaningless.

It seems that you have misunderstood what 'inline' is about.

It has a guaranteed effect and a hinting effect; read up on it.


Cheers & hth.,

- Alf

--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! Just going there is good. Linking
to it is even better! Thanks in advance!
 
Reply With Quote
 
Neelesh
Guest
Posts: n/a
 
      05-08-2009
On May 8, 3:12*pm, "Alf P. Steinbach" <(E-Mail Removed)> wrote:
> * Neelesh:
>
> > On May 8, 1:59 pm, James Kanze <(E-Mail Removed)> wrote:
> >> On May 7, 9:04 pm, Neelesh <(E-Mail Removed)> wrote:

>
> >>> On May 7, 11:36 pm, Marcelo De Brito <(E-Mail Removed)> 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.

>
> I'm sorry, the above is meaningless.
>
> It seems that you have misunderstood what 'inline' is about.
>
> It has a guaranteed effect and a hinting effect; read up on it.
>


Is "hinting effect" a technical term? I'm sorry I'm not aware of it.
Did a quick googling but that couldn't help. Also, this term doesn't
seem appear in the C++ standard

My _only_ understanding about semantics of "inline" specifier is this:

-It is a request made to the compiler and the compiler is at its own
descretion whether to obey this request or not. Of course, it will
obey if it can.
- If there are any specific cases where the compiler is "forced" to
inline, then I am not aware of such cases.
- I gather that if a function is inlined, _all_ its calls must be
substituted by its definition. I am not aware if the compilers are
allowed to selectively replace the calls that it can.

Would be greatful if you could point to a relevant resource that talks
about all these, specially the "hinting effect".

Thanks
Neelesh

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Multiple functions (one version being inline and other beingnon-inline) Rahul C++ 3 02-28-2008 03:28 PM
private virtual functions and pure virtual functions with bodies John Goche C++ 10 12-08-2006 04:00 PM
Tool which expands implicitly inline inline functions tthunder@gmx.de C++ 3 06-16-2005 12:54 AM
inline virtual functions. Dave Townsend C++ 15 05-22-2004 10:46 AM
External inline functions calling internal inline functions Daniel Vallstrom C Programming 2 11-21-2003 01:57 PM



Advertisments