 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Gino van den Bergen Guest
|
Posted: Mon Sep 13, 2004 8:22 pm Post subject: Why are calls to virtual functions not linked statically if |
|
|
I have the following situation:
An abstract class for shapes:
class Shape()
{
public:
virtual BBox bbox() const = 0;
};
A number of primitive types that implement this interface, and a class
template
template <typename Shape1, typename Shape2>
class MinkowskiSum
: public Shape
{
public:
BBox bbox() const { return m_first.bbox() + m_second.bbox(); }
private:
Shape1 m_first;
Shape2 m_second;
};
(Constructors and accessors have been left out for the sake of brevity.)
As you can see, the run-time types of the attributes of, for instance, a
MinkowskiSum<Point, Sphere>
are known at compile-time so you would expect a decent compiler to optimizie
out the dereferencing of virtual functions and to simply link the calls to
Point::bbox() and Sphere::bbox() statically into MinkowskiSum<Point,
Sphere>::bbox(). I do not see this happening in Visual C++ 7.1. There is no
optimization flag that will result in the virtual calls being removed. Now
my question: Is this a flaw in the compiler or are there other reasons why
the virtual calls cannot be removed here?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Balog Pal Guest
|
Posted: Wed Sep 15, 2004 6:40 pm Post subject: Re: Why are calls to virtual functions not linked statically |
|
|
"Gino van den Bergen" <gino (AT) dtecta (DOT) COMEAGAIN> wrote
| Quote: | BBox bbox() const { return m_first.bbox() + m_second.bbox(); }
As you can see, the run-time types of the attributes of, for instance, a
MinkowskiSum<Point, Sphere
are known at compile-time so you would expect a decent compiler to
optimizie
out the dereferencing of virtual functions and to simply link the calls to
Point::bbox() and Sphere::bbox() statically into MinkowskiSum
Sphere>::bbox(). I do not see this happening in Visual C++ 7.1.
|
Strange, but that's exattly what i see happening. Same thing without
templates. If you create a local Shape1 the compiler calls staticly, but it
seems for *member* it sticks to virtual call.
Until you find some better solution you can force a static call by
return m_first.Shape1::bbox() + m_second.Shape2::bbox();
ugly and redundancy-stuffed, but does the job.
| Quote: | Is this a flaw in the compiler or are there other reasons why
the virtual calls cannot be removed here?
|
Good question.
Paul
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michael Karcher Guest
|
Posted: Fri Sep 17, 2004 2:42 am Post subject: Re: Why are calls to virtual functions not linked statically |
|
|
Balog Pal <pasa (AT) lib (DOT) hu> wrote:
| Quote: | are known at compile-time so you would expect a decent compiler to optimizie
out the dereferencing of virtual functions and to simply link the calls to
Point::bbox() and Sphere::bbox() statically into MinkowskiSum<Point,
Sphere>::bbox(). I do not see this happening in Visual C++ 7.1.
Strange, but that's exattly what i see happening. Same thing without
templates. If you create a local Shape1 the compiler calls staticly, but it
seems for *member* it sticks to virtual call.
|
Using g++ 3.3.4 (Debian 1:3.3.4-6sarge1)
and g++ 3.4.1 (Debian 3.4.1-4sarge1) the calls get compiled statically, even
without any optimization enabled. The test source used is following the
text. Using optimization, the compiler is also able to inline the two bbox
calls. (Yes, I know the example source does not do anything useful or even
remotely related to calculating bounding boxes)
Michael Karcher
typedef int BBox;
class Shape
{
public:
virtual BBox bbox() const = 0;
};
class Sphere : public Shape
{
int radius;
public:
BBox bbox() const { return 3*radius; }
};
class Point : public Shape
{
public:
BBox bbox() const { return 1; }
};
template <typename Shape1, typename Shape2>
class MinkowskiSum : public Shape
{
public:
BBox bbox() const { return m_first.bbox() + m_second.bbox(); }
private:
Shape1 m_first;
Shape2 m_second;
};
template class MinkowskiSum<Point,Sphere>;
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gino van den Bergen Guest
|
Posted: Fri Sep 17, 2004 2:44 am Post subject: Re: Why are calls to virtual functions not linked statically |
|
|
I guess with templates it's a little harder to decide to link calls to
virtual members statically, since the substituted type could very well be a
reference. However, this still leaves the issue of why virtual calls in
non-templated classes are not linked statically.
"Balog Pal" <pasa (AT) lib (DOT) hu> wrote
| Quote: | "Gino van den Bergen" <gino (AT) dtecta (DOT) COMEAGAIN> wrote in message
news:b211$41459a7b$d468cfd2$23430 (AT) nf1 (DOT) news-service-com...
BBox bbox() const { return m_first.bbox() + m_second.bbox(); }
As you can see, the run-time types of the attributes of, for instance, a
MinkowskiSum<Point, Sphere
are known at compile-time so you would expect a decent compiler to
optimizie
out the dereferencing of virtual functions and to simply link the calls
to
Point::bbox() and Sphere::bbox() statically into MinkowskiSum
Sphere>::bbox(). I do not see this happening in Visual C++ 7.1.
Strange, but that's exattly what i see happening. Same thing without
templates. If you create a local Shape1 the compiler calls staticly, but
it
seems for *member* it sticks to virtual call.
Until you find some better solution you can force a static call by
return m_first.Shape1::bbox() + m_second.Shape2::bbox();
ugly and redundancy-stuffed, but does the job.
Is this a flaw in the compiler or are there other reasons why
the virtual calls cannot be removed here?
Good question.
Paul
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler (ne Spange Guest
|
Posted: Fri Sep 17, 2004 5:40 pm Post subject: Re: Why are calls to virtual functions not linked statically |
|
|
Hello Gino van den Bergen,
Gino van den Bergen schrieb:
| Quote: | I guess with templates it's a little harder to decide to link calls to
virtual members statically, since the substituted type could very well be a
reference.
|
Actually I don't think that this is a template-related problem, because
the compiler knows the type of
the (data) members of a given class template if it is going to
instantiate member functions of that class.
What do you think?
Greetings from Bremen,
Daniel Krügler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Dave Harris Guest
|
Posted: Sat Sep 18, 2004 2:34 pm Post subject: Re: Why are calls to virtual functions not linked statically |
|
|
[email]gino (AT) dtecta (DOT) COME[/email]AGAIN (Gino van den Bergen) wrote (abridged):
| Quote: | As you can see, the run-time types of the attributes of, for instance, a
MinkowskiSum<Point, Sphere
are known at compile-time so you would expect a decent compiler to
optimizie out the dereferencing of virtual functions and to simply
link the calls to Point::bbox() and Sphere::bbox() statically
into MinkowskiSum
|
A /very/ decent compiler might be trying to avoid code bloat. The machine
code for a vtable call to Point::bbox() is probably identical to one for
Sphere::bbox(), so the implementation of MinkowskiSum<Point, Sphere>::bbox
can probably be shared if it uses dynamic dispatch.
I doubt that's the reason here, but it could be a valid concern in some
cases.
-- Dave Harris, Nottingham, UK
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
kanze@gabi-soft.fr Guest
|
Posted: Mon Sep 20, 2004 6:28 pm Post subject: Re: Why are calls to virtual functions not linked statically |
|
|
"Gino van den Bergen" <gino (AT) dtecta (DOT) COMEAGAIN> wrote
| Quote: | I guess with templates it's a little harder to decide to link calls to
virtual members statically, since the substituted type could very well
be a reference. However, this still leaves the issue of why virtual
calls in non-templated classes are not linked statically.
|
I can't see templates playing a role here. By the time the compiler is
generating code, the temlates have been fully instantiated, and it
generates the code exactly as if it were not a template.
--
James Kanze GABI Software http://www.gabi-soft.fr
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
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|