dcsimg
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

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


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

WEBINAR:

On-Demand

Building the Right Environment to Support AI, Machine Learning and Deep Learning


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