Workaround for lack of templated typedef

Z

zr

Hi,

My original intent was to use the following code:

template <typename T> // compilation error: at typedef template is
illegal
typedef (*FunctionType)(T*,T const*,T const*,T const*,T const*, int
const*);

template <typename T, FunctionType f>
class foo
{
....
}

but the compiler complained about no templated typedef allowed.

Instead i am using the following not so aesthetic code:
template <typename T, (*f)(T*,T const*,T const*,T const*,T const*, int
const*)>
class foo
{
....
}

Now each class that inherits from foo will have this horrific template
declaration.
Is there any better way to do this?
 
P

peter koch

Hi,

My original intent was to use the following code:

template <typename T> // compilation error: at typedef template is
illegal
typedef (*FunctionType)(T*,T const*,T const*,T const*,T const*, int
const*);

template <typename T, FunctionType f>
class foo
{
...

}

but the compiler complained about no templated typedef allowed.

Instead i am using the following not so aesthetic code:
template <typename T, (*f)(T*,T const*,T const*,T const*,T const*, int
const*)>
class foo
{
...

}

Now each class that inherits from foo will have this horrific template
declaration.
Is there any better way to do this?

You could use

template< typename T >
struct func
{
typedef void (*ttype)(T const*); // or whatever
};

and then use func<T>::ttype instead of the typedef.

/Peter
 
Z

zr

You could use

template< typename T >
struct func
{
   typedef void (*ttype)(T const*); // or whatever

};

and then use func<T>::ttype instead of the typedef.

/Peter

Peter, thank you.
 
N

Noah Roberts

peter said:
You could use

template< typename T >
struct func
{
typedef void (*ttype)(T const*); // or whatever
};

and then use func<T>::ttype instead of the typedef.

/Peter

Well, not exactly. It's kind of hard to say what was intended by the OP
since the code is wrong. However, if there was a return type in the
function it would be correct. If we then traded your version for the
OP's it would actually accomplish different things.

Your version creates a typedef "ttype" that is a pointer to function
type. The OP's creates a template that requires an address to a
function of that signature and calls it "f".

Sounds like the OP needed what yours does, but it should be noted that
they don't accomplish the same thing at all.
 

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

Forum statistics

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

Latest Threads

Top