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

Tricky partial constructor specialization

Hello, I have a template with two typenames.

template <typename BASE, typename SUPER> struct Morphic
{
Morphic<BASE,SUPER>( ) { .. }
~Morphic<BASE,SUPER>( ) { .. }
};

Morphic takes a Base and a Super. Sometimes Super is itself a Morphic.
In that situation i allow a Morphic<BASE,SUPER> to be constructed from
any Morphic which uses Morphic<BASE,SUPER> as its own SUPER. I do it
like this:

template <typename S1>
Morphic<BASE,SUPER>(const Morphic<S1, Morphic<BASE,SUPER> >& copy)
{ .. }

In other words:

typedef Morphic<long, void> LongMorphic;
typedef Morphic<long, LongMorphic> LongerMorphic;

LongerMorphic hello();
LongMorphic hola(hello); // can be constructed.

I also allow construction from Morphics that are 2 or more levels
removed:

template <typename S1, typename S2>
Morphic <BASE,SUPER>(const Morphic <S2, Morphic <S1, Morphic
<BASE,SUPER> > >& copy) { .. }

Etc...so you can construct a Morphic<A,B> from a Morphic<Q, Morphic<Z,
Morphic<A,B> > >.

What I am attempting now is the reverse: construction of Morphic<Q,
Morphic<Z, Morphic<A,B> > > from Morphic<A,B>. If the compiler
understood what I meant, I would try this syntax:

template <typename S1>
Morphic<S1, Morphic<BASE,SUPER> >(const Morphic<BASE,SUPER>& copy)

Now of *course* I have two solutions already but neither is good. First
I can simply accept SUPER as a parameter:

Morphic<BASE,SUPER>(const SUPER& copy)

Works but doesn't scale to more than one level -- I can construct
Morphic<Z, Morphic<A,B> > from Morphic<A, B>, but I can't construct
Morphic<Q, Morphic<Z, Morphic<A,B> > > from Morphic<A,B>.

Another solution is to make a different implementation of the template
for each situation:

template <typename BASE, typename SUPER> Morphic;
template <typename BASE>
Morphic<BASE, void> { .. }
template <typename BASE, typename BASE2>
Morphic<BASE, Morphic<BASE2, void> > { .. }
template <typename BASE, typename BASE2, typename BASE3>
Morphic<BASE, Morphic<BASE2, Morphic<BASE3, void> > > { .. };

And have every implementation support the extent that it is. But that
seems wasteful to solve a syntax issue.

Anyone have any ideas? I'm still pretty new to metaprogramming so...:-)

Thanks in advance.
Dan

Jul 23 '05 #1
8 3693
ben
The major problem, if I understand you well, is that C++ doesn't support
template function specialization. But this can be easily achieved by
factoring out the specializing part to a standalone class. So instead of
doing

template <typename BASE, typename SUPER>
class Morphic;

template <typename NonMorphic>
class MorphicContructor{};

template <typename TMorphic>
class MorphicConstructor
{
public:
template <typename T>
static void Construct(TMorphic& morphic, const T&);
};

template <typename BASE, typename SUPER, typename S1>
class MorphicConstructor<Morphic<S1, Morphic<BASE, SUPER> > >
{
public:
static void Construct(
Morphic<S1, Morphic<BASE, SUPER> >& morphic,
const Morphic<BASE, SUPER>& copy);
};

template <typename BASE, typename SUPER>
class Morphic
{
public:
template <typename T>
Morphic(const T& copy)
{
MorphicConstructor::Construct(*this, copy);
}
};
template <typename S1>
Morphic<S1, Morphic<BASE,SUPER> >(const Morphic<BASE,SUPER>& copy)


Jul 23 '05 #2
> template <typename BASE, typename SUPER> struct Morphic
{
Morphic<BASE,SUPER>( ) { .. }
~Morphic<BASE,SUPER>( ) { .. }
};

You shouldn't have to retype the tempate params - Morphic(){...} and
~Morphic(){...} should be good enough.

I really have no idea as to why you need classes like these (specially
passing void types into parameters), but the first thing you should do
with these constructors is to make them explicit - since implicit
conversion will fail anyway due to ambiguity. And to make this work,
there is this one thing you might try, although I'm not sure about it's
success:

template <typename Base2,typename Super2>
explicit Morphic(const Morphic<Base2,Super2>& copy)
{
//...
}

template <typename Base2,typename BaseInner,typename Super2>
explicit Morphic(const Morphic<Base2,Morphic<BaseInner,Super2> >& copy)
{
//...
//Call the inner morphic somewhere, making a temporary object
Morphic<BaseInner,Super>(); //Will fail compilation if it doesn't
find
// a matched constructor, enforcing your
rules
}

Samee

Jul 23 '05 #3
Beautiful!!!

Jul 23 '05 #4
I had never heard of "explicit" before but it did do the trick of
putting the problem next to the offending line. This group is the
greatest ever.

For those interested, I used my original system for downcasting, and a
combination of both for upcasting. I thought having two systems would
cause ambiguity but xCode seems to have no problem.

By the way, I started a yahoo group for my project. It's
groups.yahoo.com/group/dgic. It's kind of a Tiny Template Library, but
oriented towards syntax sugar instead of towards a bunch of really
complicated features you don't use.

Thanks
Dan

Jul 23 '05 #5
Ugly!!!

Jul 23 '05 #6
ben
Ugly for reason!

ben
Jul 23 '05 #7
ben wrote:
Ugly for reason!


template frenzy!

Jul 23 '05 #8
ben
> template frenzy!


I just wanted to solve the OP's problem, I did it.

And yes, I am template-maniac, because it is cool and fun! : )

ben
Jul 23 '05 #9

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...
1
by: BekTek | last post by:
I'm still confused about the template partial specialization which is used in many libraries.. due to lack of introduction for beginner.. Could you tell me about that in short? Thanks in...
2
by: lhr_cool_guy | last post by:
C++ doesn't allow partial specialization of function templates. I want to know why this is so? Is the concept of function template partial specialization not well defined? I have a case where I...
6
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave...
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
9
by: Greg | last post by:
Hi, I would like to specify behavior of a class member relatively to template implemetation. It works in usual cases but it seems to fail with to templates when one of the two is specified... ...
1
by: Ioannis Gyftos | last post by:
Hello, First the code :) /////////////////////////////////////////////////////////////////////////////////// // in another header file namespace LJC{
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.