Protocol template fails in Xcode. Is there a better solution?

S

steve

Hi,

I'm trying to use a protocol class as a template parameter. The
protocol class defines its own types and methods for working with them.
The template class uses the types defined by the protocol and various
methods, however I get errors when trying to compile this in Xcode
while in Codewarrior it works fine. I'm looking for a way to either fix
this in Xcode, or find another design solution that meets the needs of
the problem.

Here is basically what I am trying to do:

----------------------
class Protocol {
public:
typedef long type;

static type Add(type val1, type val2) { return (val1 + val2); }
};

template <typename T>
class Variable {
public:
typedef T::type type; // Error in Xcode, but works in Codewarrior
//typedef long type; // Works but is not using the template type

type GetValue() { return T::Add(1, 2); }
};
----------------------
Several errors occur in Xcode when trying to compile the above code:
error: type 'T' is not derived from type 'Variable<T>'
error: expected ';' before 'type'
error: 'type' does not name a type
error: 'class Variable<Protocol>' has no member named 'GetValue'


In the end there will be a dozen protocol classes, each defining the
same named types and static methods. The only solution I have come up
with so far that works in Xcode is to explicitly define the types in
the template, such as:
template<typename T, tyepname Ttype>
but this is less than ideal since I will have more that 2 types and the
templates get used in a lot of places.

Thanks for any help!

Steve
 
R

red floyd

Hi,

I'm trying to use a protocol class as a template parameter. The
protocol class defines its own types and methods for working with them.
The template class uses the types defined by the protocol and various
methods, however I get errors when trying to compile this in Xcode
while in Codewarrior it works fine. I'm looking for a way to either fix
this in Xcode, or find another design solution that meets the needs of
the problem.

Here is basically what I am trying to do:

----------------------
class Protocol {
public:
typedef long type;

static type Add(type val1, type val2) { return (val1 + val2); }
};

template <typename T>
class Variable {
public:
typedef T::type type; // Error in Xcode, but works in Codewarrior
typedef typename T::type type;
//typedef long type; // Works but is not using the template type

type GetValue() { return T::Add(1, 2); }
};

The compiler doesn't know that the dependent name T::type is a type, so
you have to help it with the typename keyword.
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top