Sign in to follow this  
taz0010

Curiously Recurring Template Pattern

Recommended Posts

Just finished reading the article on Gamedev about it
http://www.gamedev.net/reference/articles/article2015.asp

I'm confused as to why the compiler (specifically VS2010) doesn't perform what seems to be very trivial optimisations.


class A{
public:
virtual int call()=0;
};

class B : public A{
public:
int call(){
return 10;
}
};

class C : public A{
public:
int call(){
return 50;
}
};




C* cp1 = new C();
cout << cp1->call(); // Virtual call
C c;
C* cp2 = &c;
cout << cp2->call(); // Virtual call



According to the article, cp1 and cp2 could be of types that inherit from C, so the compiler must do a VTable lookup just in case. But even if it wasn't possible to infer the type from the code above, wouldn't the compiler know that there are no classes inheriting from C? And the object can't be of type A, as A is pure virtual. Is there a technical reason for the compiler not doing this sort of optimisation?

Share this post


Link to post
Share on other sites
Quote:
Original post by taz0010
wouldn't the compiler know that there are no classes inheriting from C?


No, because it is only compiling one translation unit at a time, and the inheriting class could be in another translation unit.

Also, what does any of this have to do with CRTP?

Share this post


Link to post
Share on other sites
Quote:
No, because it is only compiling one translation unit at a time, and the inheriting class could be in another translation unit.


Oh right. Still, Whole Project Optimisation has been around for ages so I would have thought the compiler would be capable of seeing the "big picture". Do you know if GCC or Intel's compiler can do these optimisations? I was under the impression that compilers made efforts to eliminate virtual calls when possible.

Quote:
Also, what does any of this have to do with CRTP?


The article was discussing using CRTP as a method of providing compiler time polymorphism in order to eliminate the overhead of needless virtual calls.

Share this post


Link to post
Share on other sites
Quote:
Original post by taz0010
Quote:
No, because it is only compiling one translation unit at a time, and the inheriting class could be in another translation unit.


Oh right. Still, Whole Project Optimisation has been around for ages so I would have thought the compiler would be capable of seeing the "big picture". Do you know if GCC or Intel's compiler can do these optimisations? I was under the impression that compilers made efforts to eliminate virtual calls when possible.


Yes they can. I don't know if the two you mentioned do it though.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this