473,394 Members | 1,709 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,394 software developers and data experts.

implementing policy classes with template template parameters

ian
I have begun testing some code based on Chapter 1.5.1 of the book Modern C++
Design by Andrei Alexandrescu. The test code is listed below and the
compiler error message that is generated is:

testPolicy.cpp(25) : error C2984: 'CPolicy' : template parameters '<template
parameter>' and '<template parameter>' do not match
testPolicy.cpp(22) : see declaration of 'CPolicy'

I am working with version 1 of Microsoft VC++ and running WinXP Pro. Would
someone be able to point out what I am doing wrong. I've already spent
about several hours comparing the test code with that illustrated in Andrei'
book and still cannot see what the problem is.

Ian
// =====================================
template<class T>
class CPolicy
{
public:
CPolicy (void) { tValue = 1; }

T get (void) { return tValue; }
void show (void) { std::cout << "\nCPolicy" << " type = " << get(); }

private:
T tValue;
};

// =====================================
template <template <typename> class CPolicy >
class CMyPolicyClass2 : public CPolicy<int>
{
public:
void show (void) { CPolicy().show(); }
};

int main(int argc, char* argv[])
{
return 0;
}
// ===================================
my correct email address is generated by substituting 'n0spam' with
'yahoo.com'.

Jul 19 '05 #1
5 3186
On Wed, 8 Oct 2003 14:17:24 -0400, "ian" <ib***@nospam.com> wrote:
I have begun testing some code based on Chapter 1.5.1 of the book Modern C++
Design by Andrei Alexandrescu. The test code is listed below and the
compiler error message that is generated is:

testPolicy.cpp(25) : error C2984: 'CPolicy' : template parameters '<template
parameter>' and '<template parameter>' do not match
testPolicy.cpp(22) : see declaration of 'CPolicy'

I am working with version 1 of Microsoft VC++ and running WinXP Pro.
Version 1 !?!? That's circa 1990, isn't it? It has no support for
templates whatsoever.

Wouldsomeone be able to point out what I am doing wrong. I've already spent
about several hours comparing the test code with that illustrated in Andrei'
book and still cannot see what the problem is.
I've added a couple of changes with which it compiles fine on Comeau
C++ and MSVC 7.1.
// =====================================
#include <iostream>
#include <ostream>
template<class T>
class CPolicy
{
public:
CPolicy (void) { tValue = 1; }

T get (void) { return tValue; }
void show (void) { std::cout << "\nCPolicy" << " type = " << get(); }

private:
T tValue;
};

// =====================================
template <template <typename> class CPolicy >
class CMyPolicyClass2 : public CPolicy<int>
{
public:
void show (void) { CPolicy().show(); }


Should be:

void show (void) { CPolicy<int>::show(); }

Tom
Jul 19 '05 #2
ian
Hi Tom,

Thanks for the response. I'm actually working with version 1 of VC++ NET.

I've made the changes you suggested and am still getting the same compiler
error. The error reference line is always 'template <template <typename>
class CPolicy>'. I've included a complete copy of the source code below.
Would cut and paste it and let me know if it compiles correctly on your
system. Thanks for helping out.

#include "stdafx.h"

#include <iostream>

// =====================================

// =====================================

template<typename T>

class CPolicy

{

public:

CPolicy (void) { tValue = 1; }

T get (void) { return tValue; }

void show (void) { std::cout << "\nCPolicy" << " type = " << get(); }

private:

T tValue;

};

// =====================================

// =====================================

template <typename CPolicy>

class CMyPolicyClass1 : public CPolicy

{

public:

void show() { CPolicy<int>::show(); }

};

typedef CMyPolicyClass1< CPolicy<int> > CMyClass1;

// =====================================

// =====================================

template <template <typename> class CPolicy>

class CMyPolicyClass2 : public CPolicy<int>

{

public:

void show() { CPolicy<int>::show(); }

};

typedef CMyPolicyClass2< CPolicy > CMyClass2;

// =====================================

// =====================================

int main(int argc, char* argv[], char* envp[])

{

CMyClass1 oMC1;

oMC1.show();

CMyClass2 oMC2;

oMC2.show();

return 0;

}


Jul 19 '05 #3
"ian" <ib***@nospam.com> wrote in message
news:Zw*********************@weber.videotron.net.. .
I have begun testing some code based on Chapter 1.5.1 of
the book Modern C++ Design by Andrei Alexandrescu. The
test code is listed below and the compiler error message
that is generated is:
While that is a very good book to be reading, you should know
that there are some recent developments that are quite
relevent to policy-based design. Namely, MPL. The significant
thing about MPL is that it formalizes metaprogramming in a
coherent and extensible way. In particular, metafunctions are
preferred over template template parameters. Part of the
problem with template template params is that they aren't
very flexible in how they are matched. If you go with strict
metafunctions instead, you can use the template template
syntax in user code while gaining the advantages of MPL.
Also, more compilers support metafunctions than template
template params. Even those that support template template
params do not always do so correctly. Take a look at
www.boost.org, and look for MPL.
[...]
template <template <typename> class CPolicy >
class CMyPolicyClass2 : public CPolicy<int>
{
public:
void show (void) { CPolicy().show(); }
};
[...]


You declared CPolicy as a template, but invoke its c'tor
as a concrete type. A better way would be this:

template <template <typename> class CPolicy >
class CMyPolicyClass2 : public CPolicy<int>
{
public:
typedef CPolicy<int> policy_type;

void show (void) { policy_type().show(); }
};

Don't skimp on the typedefs. They can save a lot of
headaches when working with metacode. And when you
go to change something later, you'll thank yourself.

Dave

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.521 / Virus Database: 319 - Release Date: 9/23/2003
Jul 19 '05 #4
On Wed, 8 Oct 2003 18:13:15 -0400, "ian" <ib***@n0spam.com> wrote:
Hi Tom,

Thanks for the response. I'm actually working with version 1 of VC++ NET.
Do mean VC++ 7.0? The original VC++.NET? It has template template
parameters I believe, but the support for them has improved in
VC++.NET 2003 (7.1).

I've made the changes you suggested and am still getting the same compiler
error. The error reference line is always 'template <template <typename>
class CPolicy>'. I've included a complete copy of the source code below.
Would cut and paste it and let me know if it compiles correctly on your
system. Thanks for helping out.
There is one actual error, and a couple of things that cause ICEs on
VC++ 7.1. See below.

#include "stdafx.h"

#include <iostream>

// =====================================

// =====================================

template<typename T>

class CPolicy

{

public:

CPolicy (void) { tValue = 1; }

T get (void) { return tValue; }

void show (void) { std::cout << "\nCPolicy" << " type = " << get(); }

private:

T tValue;

};

// =====================================

// =====================================

template <typename CPolicy>

class CMyPolicyClass1 : public CPolicy
(1)
The above is dodgy - giving a template parameter the same name as a
type. I think it probably is legal, but it causes ICEs on VC 7.1. So
change the parameter to Policy or something.

{

public:

void show() { CPolicy<int>::show(); }
(2)
This is the actual error. Since CPolicy is a type, not a template, it
should be:

void show() { CPolicy::show(); }


};

typedef CMyPolicyClass1< CPolicy<int> > CMyClass1;

// =====================================

// =====================================

template <template <typename> class CPolicy>

class CMyPolicyClass2 : public CPolicy<int>
(3)
Again, change the name.

{

public:

void show() { CPolicy<int>::show(); }


Fine this time, since CPolicy is a template.

With just (2) it compiles on GCC and Comeau C++. With (1) and (3) as
well, it compiles fine on VC++ 7.1.

Tom
Jul 19 '05 #5
ian

"David B. Held" <dh***@codelogicconsulting.com> wrote in message
news:bm**********@news.astound.net...
"ian" <ib***@nospam.com> wrote in message
news:Zw*********************@weber.videotron.net.. .
I have begun testing some code based on Chapter 1.5.1 of
the book Modern C++ Design by Andrei Alexandrescu. The
test code is listed below and the compiler error message
that is generated is:


While that is a very good book to be reading, you should know
that there are some recent developments that are quite
relevent to policy-based design. Namely, MPL.

Dave


Hello Dave,

Thanks for the reference to MPL. I will certainly take a look at it. With
regards to the code change you made, the same compiler error continues to
occur so I will come back and look at this problem at a later date. I'm
beginning to wonder if I will have to upgrade from MSVC++ v7.0 to v7.1.

Ian

Jul 19 '05 #6

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

Similar topics

9
by: Martin Vorbrodt | last post by:
I'm designing a Matrix class of 4x4 matrices used in computer graphics. Both OpenGL and Direct3D can take a pointer to array of 16 floats which represent the values in the matrix. OGL takes it in...
4
by: Erik Wikström | last post by:
In school (no I will not ask you to do my schoolwork for me) we talked about policy-based design and got an assignment where we got the a code- fragment from a stack-implementation. The idea with...
2
by: pagekb | last post by:
Hello, I'm having some difficulty compiling template classes as containers for other template objects. Specifically, I have a hierarchy of template classes that contain each other. Template...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
4
by: aaragon | last post by:
Hello everyone. I'm reading Andrei's Alexandrescu's book on Modern C++ Design and I decided to implemente in my research code the use of policy classes. My objective is to abstract the storage of...
11
by: aaragon | last post by:
Hi everyone. I'm trying to write a class with policy based design (Alexandrescu's Modern C++ Design). I'm not a programmer but an engineer so this is kind of hard for me. Through the use of...
7
by: Amu | last post by:
Hi i am stuck up in a part of project where i have to inherit the VC++ template classes into C#.net. Please provide me the solution for this .
0
by: er | last post by:
hi, i'm trying to understand A) policy classes and see if they meet my needs. the book "C++ templates" has something similar called B) "bridge pattern implemented with templates". would it be...
3
by: DerrickH | last post by:
I have a template class with three policies: PolicyA, PolicyB, and PolicyC. The design dilemma I am having is that B and C depend upon A, but I would like my Host class to derive from all three. In...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.