Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Home » Tip Bank » C++
Language: C++
Expertise: Advanced
Jan 29, 2003

Workaround for the Lack of MSVC++ Template Support


I've been bitten by MSVC++'s inability to have templated methods without the whole class being templated and today I figured out a workaround that allows you to maintain most of the elegant design and still compiles under MSVC++.

Often, templated members are used to provide default implementation for pure virtual functions. Here's an example:
 
class CPushMe {
public:
	virtual void PushDown() = 0;
private:
	template<class TVal, class TContainer>
	static void PushDownImpl(TVal val, TContainer container)
	{
		TContainer::iterator it = container.begin();
		while( it != container.end() )
		{
			it->Apply(val);
			it->Push();
			++it
		}
	};
};

Unfortunately, the above class doesn't compile. Here's the workaround that I used in my latest project:
 
class CPushMe {
public:
	virtual void PushDown() = 0;
private:


	template<class TVal, class TContainer>
	class Impl {
		static void PushDownImpl
(TVal val, TContainer container)	{
			TContainer::iterator it = container.begin();
			while( it != container.end() )	{
				it->Apply(val);
				it->Push();
				++it
			}
		}
	};
};

Now, a generic implementation is provided and everything compiles.
 
class CPushMe2 : public CPushMe {
public:
	virtual void Push {
		Impl::PushDownImpl(m_myVal, m_someCollection);
	}
};
Mike Borozdin
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date