Problem with typelist-example on Visual Studio 2005

M

Marco Wedekind

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 T4> struct cons
{
typedef typelist<T1, typelist<T2, typelist<T3, typelist<T4,
null_typelist> > > > type;
};
template <class T1> struct cons<T1, null_typelist, null_typelist,
null_typelist>
{
typedef typelist<T1, null_typelist> type;
};
template <class T1, class T2> struct cons<T1, T2, null_typelist,
null_typelist>
{
typedef typelist<T1, typelist<T2, null_typelist> > type;
};
template <class T1, class T2, class T3> struct 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>.\templateTest.cpp(28) : error C2976: 'cons' : too few template
arguments
1> .\templateTest.cpp(11) : see declaration of 'cons'
1>.\templateTest.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\CMake\TemplateTests
\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.cpp:28: error: wrong
number of tem
plate arguments (2, should be 4)
c:/TEMP/test/CMake/TemplateTests/templateTest.cpp:11: error: provided
for `templ
ate<class T1, class T2, class T3, class T4> struct cons'
c:/TEMP/test/CMake/TemplateTests/templateTest.cpp: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
 
M

mlimber

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 T4> struct cons
{
typedef typelist<T1, typelist<T2, typelist<T3, typelist<T4,
null_typelist> > > > type;};

template <class T1> struct cons<T1, null_typelist, null_typelist,
null_typelist>
{
typedef typelist<T1, null_typelist> type;};

template <class T1, class T2> struct cons<T1, T2, null_typelist,
null_typelist>
{
typedef typelist<T1, typelist<T2, null_typelist> > type;};

template <class T1, class T2, class T3> struct 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>.\templateTest.cpp(28) : error C2976: 'cons' : too few template
arguments
1> .\templateTest.cpp(11) : see declaration of 'cons'
1>.\templateTest.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\CMake\TemplateTests
\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
 
M

Marco Wedekind

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 T4> struct cons
{
typedef typelist<T1, typelist<T2, typelist<T3, typelist<T4,
null_typelist> > > > type;};
template <class T1> struct cons<T1, null_typelist, null_typelist,
null_typelist>
{
typedef typelist<T1, null_typelist> type;};
template <class T1, class T2> struct cons<T1, T2, null_typelist,
null_typelist>
{
typedef typelist<T1, typelist<T2, null_typelist> > type;};
template <class T1, class T2, class T3> struct 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>.\templateTest.cpp(28) : error C2976: 'cons' : too few template
arguments
1> .\templateTest.cpp(11) : see declaration of 'cons'
1>.\templateTest.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\CMake\TemplateTests
\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_typeliststruct 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top