473,396 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

specialisation for templates that have template arguments

152 100+
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:
Expand|Select|Wrap|Line Numbers
  1. template<bool> class BoolClass{};
  2.  
  3. template< template<bool> class C> struct dummy;
  4.  
  5. template<> struct dummy< ... for BoolClass<true> >{};
Feb 2 '12 #1
1 1201
Banfa
9,065 Expert Mod 8TB
A basic template class with a specialisation for bool might look something like

Expand|Select|Wrap|Line Numbers
  1. template<typename T>
  2. class Store
  3. {
  4. public:
  5.     Store() : m_Data()
  6.     {
  7.     }
  8.  
  9.     ~Store()
  10.     {
  11.     }
  12.  
  13.     const T& get()
  14.     {
  15.         return m_Data;
  16.     }
  17.  
  18.     void set(const T& data)
  19.     {
  20.         std::cout << "setting store" << std::endl;
  21.         m_Data = data;
  22.     }
  23.  
  24. private:
  25.     T m_Data;
  26. };
  27.  
A boolean specialisation of it might look like (note the difference at line 20

Expand|Select|Wrap|Line Numbers
  1. template<>
  2. class Store<bool>
  3. {
  4. public:
  5.     Store() : m_Data()
  6.     {
  7.     }
  8.  
  9.     ~Store()
  10.     {
  11.     }
  12.  
  13.     const bool& get()
  14.     {
  15.         return m_Data;
  16.     }
  17.  
  18.     void set(const bool& data)
  19.     {
  20.         std::cout << "setting bool store " << std::boolalpha << data << std::endl;
  21.         m_Data = data;
  22.     }
  23.  
  24. private:
  25.     bool m_Data;
  26. };
  27.  
Feb 2 '12 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: bartek | last post by:
Please consider the following scenario below (sketch of). There are two templates defined: A and B, both with mutual conversion operators defined. Also, there's a free function template...
1
by: George Sakkis | last post by:
Hi all, I have the following two template function definitions: template <typename Container, typename Sepatator> string join(const Container& c, const Sepatator& s) { // ... } template...
4
by: sods | last post by:
Hi, I write a test code about template used for strategy. it's very similar to sample code in TC++PL 13.4.1. #include <iostream> #include <string> using std::basic_string;
3
by: danilo.horta | last post by:
Hi guys I'm trying to accomplish a slightly difficult task. I think it's more easy to explain trought an unworking code: template<class T, size_t numDim> VecBasis {/*...*/}; typedef...
1
by: Andrzej 'Foxy' D. | last post by:
Hi! I cannot figure out, why is the following code incorrect: const int array = { 1 }; const int var = 1; template<int I> struct Blah {};
4
by: Lighter | last post by:
Why is template function not allowed to have defaut arguments? We know that class template is allowed to have default arguments in C++ standard, why is template function not? I can't think out...
3
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
5
by: Kith Kanan | last post by:
In a recent thread "Why is overloading operator. (member operator) forbidden", someone wrote a code like the following: template <typename R, typename... Argtypes> class gen_send { private:...
3
by: Thomas Pajor | last post by:
Hey everybody, I got into serious trouble with template programming. I have a class which uses three template arguments, say template<typename Atype, typename Btype, typename Ctype> class...
4
by: danilo.turina | last post by:
Hi all, today I encountered a problem that I'm only able to solve by using reinterpret_cast (and I'd like to avoid it). This was my code until yesterday (omitting includes, "using namespace"...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.