Jump to content
  • Advertisement
Sign in to follow this  
yaustar

Code Generation from using C++ Templates

This topic is 4254 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If I had a template class like this:
template<class type, int maxSize>
class cRingQueue
{
public:
	cRingQueue( void );
	~cRingQueue( void );
	void						PushElement( const type & element );
	void						DequeueElement( void );
	bool						IsEmpty( void ) const;
	const type &				GetFrontElement( void ) const;

private:
	type						m_Buffer[maxSize];
	type *						m_pFirstElement;
	type * 						m_pLastElement;
	bool						m_Empty;
};
And created two objects:
cRingQueue<int, 10> testQueue1;
cRingQueue<int, 5> testQueue2;
Would the compiler generate two classes (one for the size of 10 and another for the size of 5) or just one for the storage type int? Cheers in advance.

Share this post


Link to post
Share on other sites
Advertisement
All compilers vary.

Some comilers could generate 2 sets. Some could generate 2 sets and map functions with the same body to only 1 in the final exe ( I think Mircosoft's linker will do this, with any function it finds where function bodies are indentical ).

Perhaps a compiler might generate 1 and a half sets, 1 for all the functions which don't use maxSize, and 2 for each that does.

Share this post


Link to post
Share on other sites
These are two different classes, so the compiler would have to generate them both. However, some of the duplicate code (such as the body of IsEmpty or DequeueElement) could well be eliminated in the optimization step.

Share this post


Link to post
Share on other sites
Maybe some compilers have optimised that

But first compilers acceting templates were creating 2 classes.

Don't forget : you need to implement all the template method directly inside the .h if you want to use the template in a thing.cpp that include your .h

Share this post


Link to post
Share on other sites
Quote:
Original post by ToohrVyk
These are two different classes, so the compiler would have to generate them both. However, some of the duplicate code (such as the body of IsEmpty or DequeueElement) could well be eliminated in the optimization step.

So, basically I shouldn't worry too much unless executable size becomes an issue as the compiler may have optimised a good chunk of it?

Share this post


Link to post
Share on other sites
I strongly suspect it will generate two classes. This ought to be pretty easy to check by creating an object of each class, and examining their vptr in the debugger - if different you have two classes. That's not to say that identical functions won't get stripped out in a release build, though.

It's quite rare in my experience (as in, I've never seen it and would never expect to) that the number of classes in a program cause any performances problems due to executable size. I guess that's not to say it couldn't be an issue in a constrained system, though.

Share this post


Link to post
Share on other sites
Depends on the compiler. As far as I know, GCC won't merge identical objects.
Visual C++ will do it by default (COMDAT folding I think the option is called in your linker settings)

But yes, the compiler creates two classes. But then they might get merged back into one during compilation. But during compilation they are two separate classes.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!