468,168 Members | 1,447 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,168 developers. It's quick & easy.

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_typelisttype;
};
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>.\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 T4struct 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

Apr 3 '07 #1
2 1916
On Apr 3, 11:01 am, "Marco Wedekind" <m.w...@gmx.dewrote:
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_typelisttype;};

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

Apr 3 '07 #2
On 3 Apr., 17:43, "mlimber" <mlim...@gmail.comwrote:
On Apr 3, 11:01 am, "Marco Wedekind" <m.w...@gmx.dewrote:
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_typelisttype;};
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>.\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
It works both ways and compiles :)

Cheers!

Marco

Apr 3 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Sudesh Sawant | last post: by
11 posts views Thread by Alexander Stippler | last post: by
117 posts views Thread by Peter Olcott | last post: by
reply views Thread by Jon Slaughter | last post: by
reply views Thread by James Griffiths | last post: by
6 posts views Thread by Hendrik Schober | last post: by
2 posts views Thread by Mike Collins | last post: by
3 posts views Thread by Isold.Wang | last post: by
1 post views Thread by ares.lagae | last post: by
1 post views Thread by gcdp | last post: by
reply views Thread by kamranasdasdas | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.