[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Workaround for lack of templated typedef

zr

12/8/2008 4:52:00 PM

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?
4 Answers

peter koch

12/8/2008 5:38:00 PM

0

On 8 Dec., 17:52, zr <zvir...@gmail.com> wrote:
> 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

zr

12/8/2008 6:10:00 PM

0

On Dec 8, 7:38 pm, peter koch <peter.koch.lar...@gmail.com> wrote:
> On 8 Dec., 17:52, zr <zvir...@gmail.com> wrote:
>
>
>
> > 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

Peter, thank you.

Noah Roberts

12/12/2008 6:06:00 PM

0

peter koch wrote:
> On 8 Dec., 17:52, zr <zvir...@gmail.com> wrote:
>> 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

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.

jat

6/15/2011 7:21:00 PM

0

On 06/15/2011 03:29 PM, Bufozzo wrote:
> Nadie le dijo que usted es realmente un animal malhablado, grosero,
> maleducado, pendenciero, ordinario y vulgar?
> Bueno, yo no se lo voy a decir porque me cansar?a enunciar todas esas
> caracter?sticas.
>
> Sr Bufozzo
>
>
As? es Torreloca es muy chusma...

--
/jat
Knowledge shall make you free
El conocimiento te har? libre