Problem with typelist-example on Visual Studio 2005

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

    #1

    Problem with typelist-example on Visual Studio 2005

    Hello all,

    I've just read about typelists and their implementation in C++ using
    templates in http://www.ddj.com/dept/cpp/184403813. Now my very first
    test code using typelists does not compile...

    I just cannot compile the example explained in the article on Visual
    Studio 2005 or gcc 3.3.4 (to few template arguments). I would be glad,
    if some template-guru among you could take a look at the source code
    below and check, if I simply made any syntactic errors. I've checked
    it twice, it is quite identical to the example printed in the article
    referenced above. Nobody around here could make out any error. Am I
    missing sth. here?

    So this is the source code:

    class null_typelist {};

    template <class H, class T>
    struct typelist
    {
    typedef H head;
    typedef T tail;
    };

    template <class T1, class T2, class T3, class T4struct cons
    {
    typedef typelist<T1, typelist<T2, typelist<T3, typelist<T4,
    null_typelist type;
    };
    template <class T1struct cons<T1, null_typelist, null_typelist,
    null_typelist>
    {
    typedef typelist<T1, null_typelistty pe;
    };
    template <class T1, class T2struct cons<T1, T2, null_typelist,
    null_typelist>
    {
    typedef typelist<T1, typelist<T2, null_typelist type;
    };
    template <class T1, class T2, class T3struct cons<T1, T2, T3,
    null_typelist>
    {
    typedef typelist<T1, typelist<T2, typelist<T3, null_typelist >
    type;
    };

    typedef cons<float, double>::type floating_point_ types;

    int main(int argc, char * argv[])
    {
    return 0;
    }

    ======== End of source code =============== =

    This is the error message of Visual C++:

    1>------ Build started: Project: test, Configuration: Debug Win32
    ------
    1>Compiling...
    1>templateTest. cpp
    1>.\templateTes t.cpp(28) : error C2976: 'cons' : too few template
    arguments
    1 .\templateTest. cpp(11) : see declaration of 'cons'
    1>.\templateTes t.cpp(28) : error C2955: 'cons' : use of class template
    requires template argument list
    1 .\templateTest. cpp(11) : see declaration of 'cons'
    1>Build log was saved at "file://c:\TEMP\test\CM ake\TemplateTes ts
    \test.dir\Debug \BuildLog.htm"
    1>test - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped
    ==========

    ======== End of VC++ error message =========

    This is gcc 3.4.4's error message:

    c:/TEMP/test/CMake/TemplateTests/templateTest.cp p:28: error: wrong
    number of tem
    plate arguments (2, should be 4)
    c:/TEMP/test/CMake/TemplateTests/templateTest.cp p:11: error: provided
    for `templ
    ate<class T1, class T2, class T3, class T4struct cons'
    c:/TEMP/test/CMake/TemplateTests/templateTest.cp p:28: error: `type'
    does not nam
    e a type
    make[2]: *** [CMakeFiles/test.dir/templateTest.o] Error 1
    make[1]: *** [CMakeFiles/test.dir/all] Error 2
    make: *** [all] Error 2

    ======== End of gcc error message =========

    Thanks for any hints or suggestions!

    Cheers

    Marco

  • mlimber

    #2
    Re: Problem with typelist-example on Visual Studio 2005

    On Apr 3, 11:01 am, "Marco Wedekind" <m.w...@gmx.dew rote:
    Hello all,
    >
    I've just read about typelists and their implementation in C++ using
    templates inhttp://www.ddj.com/dept/cpp/184403813. Now my very first
    test code using typelists does not compile...
    >
    I just cannot compile the example explained in the article on Visual
    Studio 2005 or gcc 3.3.4 (to few template arguments). I would be glad,
    if some template-guru among you could take a look at the source code
    below and check, if I simply made any syntactic errors. I've checked
    it twice, it is quite identical to the example printed in the article
    referenced above. Nobody around here could make out any error. Am I
    missing sth. here?
    >
    So this is the source code:
    >
    class null_typelist {};
    >
    template <class H, class T>
    struct typelist
    {
    typedef H head;
    typedef T tail;
    >
    };
    >
    template <class T1, class T2, class T3, class T4struct cons
    {
    typedef typelist<T1, typelist<T2, typelist<T3, typelist<T4,
    null_typelist type;};
    >
    template <class T1struct cons<T1, null_typelist, null_typelist,
    null_typelist>
    {
    typedef typelist<T1, null_typelistty pe;};
    >
    template <class T1, class T2struct cons<T1, T2, null_typelist,
    null_typelist>
    {
    typedef typelist<T1, typelist<T2, null_typelist type;};
    >
    template <class T1, class T2, class T3struct cons<T1, T2, T3,
    null_typelist>
    {
    typedef typelist<T1, typelist<T2, typelist<T3, null_typelist >
    type;
    >
    };
    >
    typedef cons<float, double>::type floating_point_ types;
    >
    int main(int argc, char * argv[])
    {
    return 0;
    >
    }
    >
    ======== End of source code =============== =
    >
    This is the error message of Visual C++:
    >
    1>------ Build started: Project: test, Configuration: Debug Win32
    ------
    1>Compiling...
    1>templateTest. cpp
    1>.\templateTes t.cpp(28) : error C2976: 'cons' : too few template
    arguments
    1 .\templateTest. cpp(11) : see declaration of 'cons'
    1>.\templateTes t.cpp(28) : error C2955: 'cons' : use of class template
    requires template argument list
    1 .\templateTest. cpp(11) : see declaration of 'cons'
    1>Build log was saved at "file://c:\TEMP\test\CM ake\TemplateTes ts
    \test.dir\Debug \BuildLog.htm"
    1>test - 2 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped
    ==========
    >
    ======== End of VC++ error message =========
    Try adding default parameters to your initial class template
    declaration. Either add a forward declaration like this:

    template
    <
    class T1,
    class T2 = null_typelist,
    class T3 = null_typelist,
    class T4 = null_typelist
    >
    struct cons;

    above your first specialization of cons, or make the template
    parameters in your initial definition have default parameters.

    Cheers! --M

    Comment

    • Marco Wedekind

      #3
      Re: Problem with typelist-example on Visual Studio 2005

      On 3 Apr., 17:43, "mlimber" <mlim...@gmail. comwrote:
      On Apr 3, 11:01 am, "Marco Wedekind" <m.w...@gmx.dew rote:
      >
      >
      >
      Hello all,
      >
      I've just read about typelists and their implementation in C++ using
      templates inhttp://www.ddj.com/dept/cpp/184403813. Now my very first
      test code using typelists does not compile...
      >
      I just cannot compile the example explained in the article on Visual
      Studio 2005 or gcc 3.3.4 (to few template arguments). I would be glad,
      if some template-guru among you could take a look at the source code
      below and check, if I simply made any syntactic errors. I've checked
      it twice, it is quite identical to the example printed in the article
      referenced above. Nobody around here could make out any error. Am I
      missing sth. here?
      >
      So this is the source code:
      >
      class null_typelist {};
      >
      template <class H, class T>
      struct typelist
      {
      typedef H head;
      typedef T tail;
      >
      };
      >
      template <class T1, class T2, class T3, class T4struct cons
      {
      typedef typelist<T1, typelist<T2, typelist<T3, typelist<T4,
      null_typelist type;};
      >
      template <class T1struct cons<T1, null_typelist, null_typelist,
      null_typelist>
      {
      typedef typelist<T1, null_typelistty pe;};
      >
      template <class T1, class T2struct cons<T1, T2, null_typelist,
      null_typelist>
      {
      typedef typelist<T1, typelist<T2, null_typelist type;};
      >
      template <class T1, class T2, class T3struct cons<T1, T2, T3,
      null_typelist>
      {
      typedef typelist<T1, typelist<T2, typelist<T3, null_typelist >
      type;
      >
      };
      >
      typedef cons<float, double>::type floating_point_ types;
      >
      int main(int argc, char * argv[])
      {
      return 0;
      >
      }
      >
      ======== End of source code =============== =
      >
      This is the error message of Visual C++:
      >
      1>------ Build started: Project: test, Configuration: Debug Win32
      ------
      1>Compiling...
      1>templateTest. cpp
      1>.\templateTes t.cpp(28) : error C2976: 'cons' : too few template
      arguments
      1 .\templateTest. cpp(11) : see declaration of 'cons'
      1>.\templateTes t.cpp(28) : error C2955: 'cons' : use of class template
      requires template argument list
      1 .\templateTest. cpp(11) : see declaration of 'cons'
      1>Build log was saved at "file://c:\TEMP\test\CM ake\TemplateTes ts
      \test.dir\Debug \BuildLog.htm"
      1>test - 2 error(s), 0 warning(s)
      ========== Build: 0 succeeded, 1 failed, 1 up-to-date, 0 skipped
      ==========
      >
      ======== End of VC++ error message =========
      >
      Try adding default parameters to your initial class template
      declaration. Either add a forward declaration like this:
      >
      template
      <
      class T1,
      class T2 = null_typelist,
      class T3 = null_typelist,
      class T4 = null_typelist
      >
      struct cons;
      >
      above your first specialization of cons, or make the template
      parameters in your initial definition have default parameters.
      >
      Cheers! --M
      It works both ways and compiles :)

      Cheers!

      Marco

      Comment

      Working...