implementing policy classes with template template parameters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ian

    implementing policy classes with template template parameters

    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'.



  • tom_usenet

    #2
    Re: implementing policy classes with template template parameters

    On Wed, 8 Oct 2003 14:17:24 -0400, "ian" <ib252@nospam.c om> wrote:
    [color=blue]
    >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.[/color]

    Version 1 !?!? That's circa 1990, isn't it? It has no support for
    templates whatsoever.

    Would[color=blue]
    >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.[/color]

    I've added a couple of changes with which it compiles fine on Comeau
    C++ and MSVC 7.1.
    [color=blue]
    >// =============== =============== =======[/color]

    #include <iostream>
    #include <ostream>
    [color=blue]
    >template<cla ss 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( ); }[/color]

    Should be:

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

    Tom

    Comment

    • ian

      #3
      Re: implementing policy classes with template template parameters

      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<typena me 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>::s how(); }

      };

      typedef CMyPolicyClass1 < CPolicy<int> > CMyClass1;

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

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

      template <template <typename> class CPolicy>

      class CMyPolicyClass2 : public CPolicy<int>

      {

      public:

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

      };

      typedef CMyPolicyClass2 < CPolicy > CMyClass2;

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

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

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

      {

      CMyClass1 oMC1;

      oMC1.show();

      CMyClass2 oMC2;

      oMC2.show();

      return 0;

      }




      Comment

      • David B. Held

        #4
        Re: implementing policy classes with template template parameters

        "ian" <ib252@nospam.c om> wrote in message
        news:ZwYgb.9568 8$282.1634777@w eber.videotron. net...[color=blue]
        > 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:[/color]

        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.
        [color=blue]
        > [...]
        > template <template <typename> class CPolicy >
        > class CMyPolicyClass2 : public CPolicy<int>
        > {
        > public:
        > void show (void) { CPolicy().show( ); }
        > };
        > [...][/color]

        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().s how(); }
        };

        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


        Comment

        • tom_usenet

          #5
          Re: implementing policy classes with template template parameters

          On Wed, 8 Oct 2003 18:13:15 -0400, "ian" <ib252@n0spam.c om> wrote:
          [color=blue]
          >Hi Tom,
          >
          >Thanks for the response. I'm actually working with version 1 of VC++ NET.[/color]

          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).
          [color=blue]
          >
          >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.[/color]

          There is one actual error, and a couple of things that cause ICEs on
          VC++ 7.1. See below.
          [color=blue]
          >
          >#include "stdafx.h"
          >
          >#include <iostream>
          >
          >// =============== =============== =======
          >
          >// =============== =============== =======
          >
          >template<typen ame 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[/color]

          (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.
          [color=blue]
          >
          >{
          >
          >public:
          >
          >void show() { CPolicy<int>::s how(); }[/color]

          (2)
          This is the actual error. Since CPolicy is a type, not a template, it
          should be:

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

          [color=blue]
          >
          >};
          >
          >typedef CMyPolicyClass1 < CPolicy<int> > CMyClass1;
          >
          >// =============== =============== =======
          >
          >// =============== =============== =======
          >
          >template <template <typename> class CPolicy>
          >
          >class CMyPolicyClass2 : public CPolicy<int>[/color]

          (3)
          Again, change the name.
          [color=blue]
          >
          >{
          >
          >public:
          >
          >void show() { CPolicy<int>::s how(); }[/color]

          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

          Comment

          • ian

            #6
            Re: implementing policy classes with template template parameters


            "David B. Held" <dheld@codelogi cconsulting.com > wrote in message
            news:bm36g8$o7p $1@news.astound .net...[color=blue]
            > "ian" <ib252@nospam.c om> wrote in message
            > news:ZwYgb.9568 8$282.1634777@w eber.videotron. net...[color=green]
            > > 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:[/color]
            >
            > 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
            >[/color]

            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



            Comment

            Working...