473,410 Members | 1,889 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,410 software developers and data experts.

Problem with BEGIN_TEMPLATE_MESSAGE_MAP (long)

Hi,

Consider the following code:

---

template <class T>
class TMyCWndWrapper : public T
{
public:
afx_msg void OnEnable(BOOL bEnable)
{
// For the sake of simplicity I'll just call the base class imp.
T::OnEnable(bEnable);
}

DECLARE_MESSAGE_MAP()
};

// Define a custom template IMPLEMENT_MESSAGE_MAP
#define WND_TEMPLATE_IMPLEMENT_MESSAGE_MAP(baseClass) \
BEGIN_MESSAGE_MAP(TMyCWndWrapper< baseClass >, baseClass) \
ON_WM_ENABLE() \
END_MESSAGE_MAP()
WND_TEMPLATE_IMPLEMENT_MESSAGE_MAP( CListBox );
WND_TEMPLATE_IMPLEMENT_MESSAGE_MAP( CListCtrl );

---

This code compiles just fine in VC6.
In VC8, I get the following error:
error C2906: 'const AFX_MSGMAP
*TMyCWndWrapper<T>::GetThisMessageMap(void)' : explicit specialization
requires 'template <>'

So I went on and implemented my own BEGIN_TEMPLATE_MESSAGE_MAP and
added the "template <>" where needed.

When I compiled, I had a macro redefinition on
BEGIN_TEMPLATE_MESSAGE_MAP. It turns out that MFC already took care of
that, so I removed my implementation and used MFC's with the adjusted
line:
BEGIN_TEMPLATE_MESSAGE_MAP(TMyCWndWrapper, baseClass , baseClass)

Now, if I define only the first line:
WND_TEMPLATE_IMPLEMENT_MESSAGE_MAP( CListBox );

The code actually compiles, but if I add the other line:
WND_TEMPLATE_IMPLEMENT_MESSAGE_MAP( CListCtrl );

Now I get the error:
error C2995: 'const AFX_MSGMAP *TMyCWndWrapper<T>::GetMessageMap(void)
const' : function template has already been defined.
My current status is that I re-implemented BEGIN_TEMPLATE_MESSAGE_MAP
as follows:

---
#undef BEGIN_TEMPLATE_MESSAGE_MAP
#define BEGIN_TEMPLATE_MESSAGE_MAP(theClass, type_name, baseClass) \
PTM_WARNING_DISABLE \
template <\
const AFX_MSGMAP* theClass< type_name >::GetMessageMap() const \
{ return GetThisMessageMap(); } \
template <\
const AFX_MSGMAP* PASCAL theClass< type_name >::GetThisMessageMap() \
{ \
typedef theClass< type_name ThisClass; \
typedef baseClass TheBaseClass; \
static const AFX_MSGMAP_ENTRY _messageEntries[] = \
{
---

The diff. between this one and MFC's is that I replaced:
template < typename type_name >
with:
template <>

---

The code now compiles just fine.
So, in case this helps someone, I'll be very happy.
Beyond that, if someone would like to explain what's going on here I
will be happy to receive an answer.
I don't understand what "template < typename type_name >" means.

Cheers.

Dec 13 '06 #1
3 4408
---
#undef BEGIN_TEMPLATE_MESSAGE_MAP
#define BEGIN_TEMPLATE_MESSAGE_MAP(theClass, type_name, baseClass) \
PTM_WARNING_DISABLE \
template <\
const AFX_MSGMAP* theClass< type_name >::GetMessageMap() const \
{ return GetThisMessageMap(); } \
template <\
const AFX_MSGMAP* PASCAL theClass< type_name >::GetThisMessageMap() \
{ \
typedef theClass< type_name ThisClass; \
typedef baseClass TheBaseClass; \
static const AFX_MSGMAP_ENTRY _messageEntries[] = \
{
---

The diff. between this one and MFC's is that I replaced:
template < typename type_name >
with:
template <>

---

The code now compiles just fine.
So, in case this helps someone, I'll be very happy.
Beyond that, if someone would like to explain what's going on here I
will be happy to receive an answer.
I don't understand what "template < typename type_name >" means.
The difference is that your code implements a single specialized message map
each time, whereas the library code declares an entire family of message
maps using a template parameter. With the library version, you aren't
actually specifying what the base class is... instead you are giving a name
to the base class which can be used inside the message map to declare
functions that use the base class, whatever it may be, in the argument list
or return type.
>
Cheers.

Dec 13 '06 #2
Ben Voigt wrote:
>
The difference is that your code implements a single specialized message map
each time, whereas the library code declares an entire family of message
maps using a template parameter. With the library version, you aren't
actually specifying what the base class is... instead you are giving a name
to the base class which can be used inside the message map to declare
functions that use the base class, whatever it may be, in the argument list
or return type.
I'm doing my best to understand what you wrote, but I'm having a hard
time connecting it to real life.
Can you please give a usage example to MFC's version of the macro? I'm
just missing its purpose.
Also, am I on the right track, or do you have a better idea for me to
solve my issue using standard macros instead of redefining it?

TIA.

Dec 14 '06 #3

<no********@gmail.comwrote in message
news:11*********************@j72g2000cwa.googlegro ups.com...
Ben Voigt wrote:
>>
The difference is that your code implements a single specialized message
map
each time, whereas the library code declares an entire family of message
maps using a template parameter. With the library version, you aren't
actually specifying what the base class is... instead you are giving a
name
to the base class which can be used inside the message map to declare
functions that use the base class, whatever it may be, in the argument
list
or return type.

I'm doing my best to understand what you wrote, but I'm having a hard
time connecting it to real life.
Can you please give a usage example to MFC's version of the macro? I'm
just missing its purpose.
Also, am I on the right track, or do you have a better idea for me to
solve my issue using standard macros instead of redefining it?
You have something along these lines you wrote:

// Define a custom template IMPLEMENT_MESSAGE_MAP
#define WND_TEMPLATE_IMPLEMENT_MESSAGE_MAP(baseClass) \
BEGIN_MESSAGE_MAP(TMyCWndWrapper< baseClass >, baseClass) \
ON_WM_ENABLE() \
END_MESSAGE_MAP()
WND_TEMPLATE_IMPLEMENT_MESSAGE_MAP( CListBox );
WND_TEMPLATE_IMPLEMENT_MESSAGE_MAP( CListCtrl );
With the MFC map, you get the per-class repetition for free as part of the
template behavior, no macro needed.

BEGIN_TEMPLATED_MESSAGE_MAP(TMyCWndWrapper, baseClass, baseClass) \
ON_WM_ENABLE()
END_MESSAGE_MAP()

defines the message map for TMyCWndWrapper<baseClassfor all needed values
of baseClass. The compiler will instantiate the template for both
TMyCWndWrapper<CListBoxand TMyCWndWrapper<CListCtrlif you have used them
anywhere. This is far better than the macro because: it is type-safe and
error reporting is far better, and there is no need to maintain the list of
instances.

or to see why there need to be three arguments instead of two, add to the
above:
template <typename T>
class TMyDerivedWnd : TMyCWndWrapper<T{};

BEGIN_TEMPLATED_MESSAGE_MAP(TMyDerivedWnd, baseClass,
TMyCWndWrapper<baseClass>) \
ON_WM_ENABLE()
END_MESSAGE_MAP()

>
TIA.

Dec 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sajidazmi | last post by:
Hi, I'm coding an Stock Order Entry System. The problem I'm facing is when I'm passing a pointer to one class, the pointer gets corrupted. I'm not able to see why? Here is the code, please tell...
4
by: MrTang001 | last post by:
How I can fix this problem? I don't know why it alway prompted (first use this function). I have use newDollars, newCents... to access the base class member variable. And the member functions for...
1
by: Rohit Raghuwanshi | last post by:
Hello all, we are running a delphi application with DB2 V8.01 which is causing deadlocks when rows are being inserted into a table. Attaching the Event Monitor Log (DEADLOCKS WITH DETAILS) here....
6
by: Uttam | last post by:
Hello, I am using the code from Chapter 17 specifically the code from the font frmListFonts in my application. I have taken care to copy all the relevant modules / class modules into the...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
7
by: Camellia | last post by:
hi all, I wrote a "table of powers" program and made the 5th power the highest, but when I tried to run the program I found a problem. Here's the output: Integer Square power...
8
by: Aereshaa | last post by:
For some reason, when I compile this code: int main(){ char* a = malloc(5); long* l = (*long) a; } //I shortened it to isolate the problem. I get this error: error.c:3: error: expected...
1
by: Alex Vinokur | last post by:
Hi, I have compilation problem on SUN CC compiler with template while using option -m64 (64-bit addressing model). No problem while using option -m32 (32-bit addressing model) $ CC -V CC:...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.