Convoluted template declarations

  • Thread starter Robert J. Hansen
  • Start date
R

Robert J. Hansen

I'm exploring the use of type traits (which, I hasten to say, I've
hardly ever used before) as a way to help simplify some complex
special-case logic in an existing codebase. I'm finding that I'm
running into some of the sharp corners of the C++ template facility.
I'm hoping someone can answer a couple of questions about the following
code snippet:

=====

template <
class D, template <D> class S,
struct MetaTraits {
typedef typename D Data;
typedef typename S Solution;
typedef typename E Engine;
};

=====

.... I've tried pretty much every variation I can on this, and for
naught. Does anyone know the necessary incantation?

The task is to produce a framework for AI problems which can be
addressed by search methods. Any given engine for computing solutions
will need to know the type of the data it's operating upon, and the
type of the solutions it's generating: hence the existence of
ProblemTraits. Likewise, there are many kinds of engines which have
their solutions generate new potentials in the same neighborhood, which
means their solutions have to be able to reflect into the data.
Finally, the runtime system needs to know the types of all three
components (data, solutions and engine), hence the metatrait which
encompasses ProblemTrait<D, S> and Engine<ProblemTrait<D, S> >.

Of course, trying to hook up incompatible Engines to incompatible
ProblemTraits should cause the compile to blow up, as will incompatible
solutions to incompatible data--that's a feature I'm looking forward
to, since right now we're catching these things at runtime.

Thanks in advance for any help you can give. :)
 
J

John Harrison

Robert said:
I'm exploring the use of type traits (which, I hasten to say, I've
hardly ever used before) as a way to help simplify some complex
special-case logic in an existing codebase. I'm finding that I'm
running into some of the sharp corners of the C++ template facility.
I'm hoping someone can answer a couple of questions about the following
code snippet:

=====

template <
class D, template <D> class S,
template<D, S> struct ProblemTraits,
template<ProblemTraits> struct E

struct MetaTraits {
typedef typename D Data;
typedef typename S Solution;
typedef typename E Engine;
};

=====

... I've tried pretty much every variation I can on this, and for
naught. Does anyone know the necessary incantation?

Well it's kind of hard to follow what you are wanting to do but

1) struct is illegal in the above code, it must be class
2) the syntax of your template template parameters is wrong (I assume
that is what you want)
3) typename is unnecessary in the typedefs
4) You must supply parameters for S and E in the typedefs

I would say that what you want is this

template <
class D,
template <class> class S,
template <class, class> class ProblemTraits,
template <class> class E>
struct MetaTraits {
typedef D Data;
typedef S<D> Solution;
typedef E< ProblemTraits< D, S<D> > > Engine;
};

It does at least compile.

john
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top