specialisation for templates that have template arguments

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tassos Souris
    New Member
    • Aug 2008
    • 152

    specialisation for templates that have template arguments

    we have a class template that has a template argument and want to specialize it..
    anyone has any examples for this? (haven't any particular case in mind just want to see how this is done to get it working)

    e.g:
    Code:
    template<bool> class BoolClass{};
    
    template< template<bool> class C> struct dummy;
    
    template<> struct dummy< ... for BoolClass<true> >{};
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    A basic template class with a specialisation for bool might look something like

    Code:
    template<typename T>
    class Store
    {
    public:
    	Store() : m_Data()
    	{
    	}
    	
    	~Store()
    	{
    	}
    	
    	const T& get()
    	{
    		return m_Data;
    	}
    	
    	void set(const T& data)
    	{
    		std::cout << "setting store" << std::endl;
    		m_Data = data;
    	}
    	
    private:
    	T m_Data;
    };
    A boolean specialisation of it might look like (note the difference at line 20

    Code:
    template<>
    class Store<bool>
    {
    public:
    	Store() : m_Data()
    	{
    	}
    	
    	~Store()
    	{
    	}
    	
    	const bool& get()
    	{
    		return m_Data;
    	}
    	
    	void set(const bool& data)
    	{
    		std::cout << "setting bool store " << std::boolalpha << data << std::endl;
    		m_Data = data;
    	}
    	
    private:
    	bool m_Data;
    };

    Comment

    Working...