473,386 Members | 1,621 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,386 software developers and data experts.

Simple specialization problem

Hello,

I am trying to express a very simple piece of code. I have:

#include <iostream>

enum A { a1, a2, a3, a4 };
enum B { b1, b2, b3, b4 };

template <int TA, int TBstruct Foo {
void foo() { std::cout << "general\n"; }
};

Now I want to say : only allow TB=b1 when TA=a1:

template<struct Foo<a1,b1{
void foo() { std::cout << "duplicated\n"; }
};
// mark everything else illegal:
template<int TBstruct Foo<a1, TB>;

This is working for me. The problem is that I need to duplicate the
code for Foo<a1,b1when I simply want to use the general
implementation. Is there a way to say:

template <struct Foo <a1,b1: public Foo<a1,b1?

Or is there another way to achieve a compilation error for Foo <a1, `b!
=b1`>

Thanks
-Mathieu

Feb 14 '07 #1
1 1349
mathieu wrote:
Hello,

I am trying to express a very simple piece of code. I have:

#include <iostream>

enum A { a1, a2, a3, a4 };
enum B { b1, b2, b3, b4 };

template <int TA, int TBstruct Foo {
void foo() { std::cout << "general\n"; }
};

Now I want to say : only allow TB=b1 when TA=a1:

template<struct Foo<a1,b1{
void foo() { std::cout << "duplicated\n"; }
};
// mark everything else illegal:
template<int TBstruct Foo<a1, TB>;

This is working for me. The problem is that I need to duplicate the
code for Foo<a1,b1when I simply want to use the general
implementation. Is there a way to say:

template <struct Foo <a1,b1: public Foo<a1,b1?

Or is there another way to achieve a compilation error for Foo <a1, `b!
=b1`>

Thanks
-Mathieu
You could use boost's metaprogramming library. Something like:

#include <iostream>
#include <boost/static_assert.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/not_equal_to.hpp>
#include <boost/mpl/comparison.hpp>
#include <boost/mpl/or.hpp>
#include <boost/mpl/logical.hpp>
#include <boost/mpl/assert.hpp>

enum A { a1, a2, a3, a4 };
enum B { b1, b2, b3, b4 };

using namespace boost::mpl ;

template <int TA, int TBstruct Foo {

BOOST_MPL_ASSERT(( or_< not_equal_to< int_<TA>, int_<a1 ,
equal_to< int_<TB>, int_<b1 )) ;
void foo() { std::cout << "general\n"; }
};

--
Alan Johnson
Feb 14 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
9
by: Philip Lawatsch | last post by:
Hi I'd like to implement some kind if type traits myself, but I have to support broken compilers (like visual studio) that do not support Partial Specialization. My first shot was something...
8
by: Agent Mulder | last post by:
Hi group, I have a problem with partial template specialization. In the code below I have a template struct Music with one method, play(), and three kinds of music, Jazz, Funk and Bach. When I...
5
by: __PPS__ | last post by:
Hello, I want to write specialized method for a class: template<class A, class B> class xxx{ A a; B b; operator bool()const{ a==b; } };
19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
3
by: Scott Frazer | last post by:
I'm trying to do some template specialization and can't get it to work quite right. I have a templated base class: template <typename T> class SignalBase { ... }; and a derived class: ...
2
by: Yang Zhang | last post by:
I have a small program like this: //////////////////////////////////////////////// #include <iostream> using namespace std ; // set bits in an address template <typename T> inline T*...
7
by: aaragon | last post by:
Hi everyone, The idea is quite simple: generate a container with random values in it. For that, I decided to create a class that I called RandomContainer that inherits from a container (with...
6
by: abir | last post by:
i have a template as shown template<typename Sclass Indexer{}; i want to have a specialization for std::vector both const & non const version. template<typename T,typename Aclass...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.