C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

template and forward declaration

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
jobseeker
Guest





PostPosted: Tue Oct 14, 2003 9:21 pm    Post subject: template and forward declaration Reply with quote



I have defined a class that contains a data member of a function type.
The signature of the function type take a pointer of the class itself
as a parameter:

class Softkey; // forward declaration

typedef int (* pfncOnSelect)(Softkey *key, void *);

class Softkey
{

...
int value;
pfncOnSelect m_OnSelect;
...
};

This class is compiled and run successfully without error. The
problem I have now is that I am trying to modify this class into a
templated class as follows:

template<class Type>
class SoftKey; // forward declaration.

typedef int (* pfncOnSelect)(SoftKey *key, void *);

template<class Type>
class Softkey
{

...
Type value;
pfncOnSelect m_OnSelect;
...
};

Now the compiler complains about syntax error in the typedef line.
I have tried with

template<class Type>
typedef int (* pfncDrawCap)(TestKey *key, void *);

or

template<class Type>
typedef int (* pfncDrawCap)(TestKey<Type> *key, void *);

but I still get an error. Can anyone throw some light into this?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Ben Hutchings
Guest





PostPosted: Thu Oct 16, 2003 2:10 pm    Post subject: Re: template and forward declaration Reply with quote



In article <6bb7c82b.0310141309.40ba4c47 (AT) posting (DOT) google.com>,
jobseeker wrote:
<snip>
Quote:
The problem I have now is that I am trying to modify this class into a
templated class as follows:

template<class Type
class SoftKey; // forward declaration.

typedef int (* pfncOnSelect)(SoftKey *key, void *);

template class Softkey
{

...
Type value;
pfncOnSelect m_OnSelect;
...
};

Now the compiler complains about syntax error in the typedef line.

Right, SoftKey is not a type name.

Quote:
I have tried with

template typedef int (* pfncDrawCap)(TestKey *key, void *);

or

template typedef int (* pfncDrawCap)(TestKey
but I still get an error. Can anyone throw some light into this?

There is no such thing as a template typedef. Also, you have
inconsistently changed names in your code.

In the absence of a template typedef or similar feature, you can
use either this:

template<class Type>
class SoftKey
{
typedef int (* pfncOnSelect)(SoftKey *key, void *);
Type value;
pfncOnSelect m_OnSelect;
};

(replacing pfncOnSelect with SoftKey<Type>::pfncOnSelect)
or this:

template<class Type>
class SoftKey;

template<class Type>
struct pfncOnSelect
{
typedef int (* type)(SoftKey<Type> *key, void *);
};

template<class Type>
class SoftKey
{
Type value;
typename pfncOnSelect<Type>::type m_OnSelect;
};

(replacing pfncOnSelect with pfncOnSelect<Type>::type).

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Siemel Naran
Guest





PostPosted: Thu Oct 16, 2003 2:22 pm    Post subject: Re: template and forward declaration Reply with quote



"jobseeker" <jobseeker95479 (AT) yahoo (DOT) com> wrote in message

Quote:
template<class Type
class SoftKey; // forward declaration.

typedef int (* pfncOnSelect)(SoftKey *key, void *);

template typedef int (* pfncDrawCap)(TestKey *key, void *);

In the above and below, what is 'TestKey'? Did you mean 'SoftKey'?

Quote:
template typedef int (* pfncDrawCap)(TestKey

The language does not support template typedefs. There is a close
workaround though: a template struct, with a typedef in it.

template<class Type>
struct pfncDrawCap {
typedef int (* FunctionPtr)(SoftKey<Type> *key, void *);
};

And then

template<class Type>
class Softkey
{

...
Type value;
typename pfncOnSelect<Type>::FunctionPtr m_OnSelect;
...
};

You could perhaps make struct pfncDrawCap a nested struct, that is
Softkey::pfncDrawCap.

--
+++++++++++
Siemel Naran


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Ron
Guest





PostPosted: Thu Oct 16, 2003 2:28 pm    Post subject: Re: template and forward declaration Reply with quote

Quote:
I have defined a class that contains a data member of a function type.
The signature of the function type take a pointer of the class itself
as a parameter:

class Softkey; // forward declaration

typedef int (* pfncOnSelect)(Softkey *key, void *);

class Softkey
{

...
int value;
pfncOnSelect m_OnSelect;
...
};

This class is compiled and run successfully without error. The
problem I have now is that I am trying to modify this class into a
templated class as follows:

template class SoftKey; // forward declaration.

typedef int (* pfncOnSelect)(SoftKey *key, void *);

template class Softkey
{

...
Type value;
pfncOnSelect m_OnSelect;
...
};

Now the compiler complains about syntax error in the typedef line....

Yep. C++ doesn't support templated typedefs. One alternative would be
to derive SoftKey from a class that supports all the methods you need,
then override those methods in SoftKey. Then you could write you
typedef as

typedef int (* pfncOnSelect)(SoftKeyBase *keyBase, void *);

-Ron

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Christoph Schulz
Guest





PostPosted: Thu Oct 16, 2003 2:31 pm    Post subject: Re: template and forward declaration Reply with quote

Hello!

jobseeker <jobseeker95479 (AT) yahoo (DOT) com> wrote:

Quote:
I have defined a class that contains a data member of a function type.
The signature of the function type take a pointer of the class itself
as a parameter:

class Softkey; // forward declaration

typedef int (* pfncOnSelect)(Softkey *key, void *);

class Softkey
{
[snip]
};

This class is compiled and run successfully without error. The
problem I have now is that I am trying to modify this class into a
templated class as follows:

template<class Type
class SoftKey; // forward declaration.

typedef int (* pfncOnSelect)(SoftKey *key, void *);

This doesn't work because Softkey does not denote a class but a
class template.

Quote:
[snip]

Now the compiler complains about syntax error in the typedef line.
I have tried with

template typedef int (* pfncDrawCap)(TestKey *key, void *);

or

template typedef int (* pfncDrawCap)(TestKey
but I still get an error. Can anyone throw some light into this?

You can't name a type via typedef which depends on a template parameter.
So any use of "template <...> typedef ..." is illegal.

You can fix your example by moving the typedef into the template class,
where it is apparently used to define the member m_onSelect:

template<class Type>
class Softkey
{
typedef int (* pfncOnSelect)(Softkey *key, void *);
Type value;
pfncOnSelect m_OnSelect;
}

This works because "Softkey" in the typedef is really "Softkey<Type>".
So you defined a distinctive typedef for each possible Softkey
specialization. Softkey<int>::pfncOnSelect is different from
Softkey<char>::pfncOnSelect, for instance. But that's understandable,
because Softkey<int> is not Softkey<char> either.

To summarize, every time you need a templated typedef you can move
it into a templated class. E.g. if you need something like:

template <class T1, class T2> typedef T1 (*myTypedef) (T2);

you'll need to write:

template <class T1, class T2> struct myTypedef {
typedef T1 (*type) (T2);
};

and you can use it as follows:

myTypedef <int, char>::type callback;


Regards,
Christoph



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tony Oliver
Guest





PostPosted: Thu Oct 16, 2003 5:28 pm    Post subject: Re: template and forward declaration Reply with quote

[email]jobseeker95479 (AT) yahoo (DOT) com[/email] (jobseeker) wrote

Quote:
I have defined a class that contains a data member of a function type.
The signature of the function type take a pointer of the class itself
as a parameter:

class Softkey; // forward declaration

typedef int (* pfncOnSelect)(Softkey *key, void *);

class Softkey
{

...
int value;
pfncOnSelect m_OnSelect;
...
};

This class is compiled and run successfully without error. The
problem I have now is that I am trying to modify this class into a
templated class as follows:

template<class Type
class SoftKey; // forward declaration.

typedef int (* pfncOnSelect)(SoftKey *key, void *);

template class Softkey
{

...
Type value;
pfncOnSelect m_OnSelect;
...
};

Now the compiler complains about syntax error in the typedef line.
I have tried with

template typedef int (* pfncDrawCap)(TestKey *key, void *);

or

template typedef int (* pfncDrawCap)(TestKey
but I still get an error. Can anyone throw some light into this?

You're nearly there. Unfortunately, you (like the rest of us) want
templated typedefs, which are currently not part of the language (but
I believe this is an issue under discussion for possible inclusion in
the next Standard).

There is a well-known trick to circumvent this limitation (it's even
used in the Standard Template Library). You have to wrap your typedef
inside a templated structure as follows:

template<class Type>
class SoftKey; // forward declaration.

template<class Type>
struct Specialised_On
{
typedef int (*pfncOnSelect)(SoftKey<Type> *key, void *);
};

template<class Type>
class Softkey
{
//...
Type value;
Specialised_On<Type>::pfncOnSelect m_OnSelect;
//...
};

Hope this helps.

Best regards,

Tony.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Sebastian Faust
Guest





PostPosted: Fri Oct 17, 2003 2:29 am    Post subject: Re: template and forward declaration Reply with quote

Hi,

try the follwing:

template<class Type>
class SoftKey; // forward declaration.

typedef int (* pfncOnSelect)(SoftKey<class Type> *key, void *);

template<class Type>
class Softkey
{

Type value;
pfncOnSelect m_OnSelect;

};

Bye,
Sebastian



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Thomas Mang
Guest





PostPosted: Sun Oct 19, 2003 9:42 pm    Post subject: Re: template and forward declaration Reply with quote



Tony Oliver schrieb:

Quote:
You're nearly there. Unfortunately, you (like the rest of us) want
templated typedefs, which are currently not part of the language (but
I believe this is an issue under discussion for possible inclusion in
the next Standard).

There is a well-known trick to circumvent this limitation (it's even
used in the Standard Template Library). You have to wrap your typedef
inside a templated structure as follows:

template<class Type
class SoftKey; // forward declaration.

template struct Specialised_On
{
typedef int (*pfncOnSelect)(SoftKey };

template<class Type
class Softkey
{
//...
Type value;
Specialised_On

Almost correct. This line should start with a "typename", since it depends on
a template parameter:

typename Specialised_On<Type>::pfncOnSelect m_OnSelect;

Quote:

//...
};


regards,

Thomas


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Tony Oliver
Guest





PostPosted: Mon Oct 20, 2003 10:03 pm    Post subject: Re: template and forward declaration Reply with quote

Thomas Mang <a9804814 (AT) unet (DOT) univie.ac.at> wrote
Quote:
Tony Oliver schrieb:
SNIP
template<class Type
class Softkey
{
//...
Type value;
Specialised_On
Almost correct. This line should start with a "typename", since it depends on
a template parameter:

typename Specialised_On<Type>::pfncOnSelect m_OnSelect;


//...
};


regards,

Thomas

D'oh! Thanks, Thomas.

I forgot to re-test this under g++; I must have (uncharacteristically)
only tested this under MSVC6, which fails to warn about the deprecated
nature of compiler-deduced types taken from template arguments.
Typical...

Tony.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.