I don't understand why this doesn't compile:
struct A
{};
template<class T>
struct B
{};
template<template<class> class T1, class T2>
struct C
{};
int
main (int ac, char **av)
{
typedef B<double> b; //compiles
typedef B<const double> b_const; //compiles
typedef B<A> ba; //compiles
typedef B<const A> ba_const; //compiles
typedef C<B,double> c1; //compiles
typedef C<B,const double> c2; //compiles
typedef C<const B,double> c3; //ISO C++ forbids declaration of ‘type name’ with no type
}
(I find the reference to the standard a little cryptic)
What do I have to change to make it compile?
EDIT:
Compiler details (it seems to be relevent):
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.4.4-14ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)
EDIT2:
By means of explaination, I am trying to do something like this:
template<template<class> class TheContainer, class T>
struct Iterator
template<class T>
struct Container
typedef Iterator<Container, double> iterator;
typedef Iterator<const Container, double> const_iterator;
The technique for non-templated containers is found at the end of this boost doc: http://www.boost.org/doc/libs/1_46_1/libs/iterator/doc/iterator_facade.html
I guess the solution is not to nestle the templates. In retrospect it seems obvious.
Template classes and functions can make use of another kind of template parameter known as a non-type parameter. A template non-type parameter is a template parameter where the type of the parameter is predefined and is substituted for a constexpr value passed in as an argument.
Templates can be template parameters. In this case, they are called template parameters. The container adaptors std::stack, std::queue, and std::priority_queue use per default a std::deque to hold their arguments, but you can use a different container.
Template parameters may have default arguments. The set of default template arguments accumulates over all declarations of a given template.
8. Why we use :: template-template parameter? Explanation: It is used to adapt a policy into binary ones.
The first argument to C isn't a type, hence it makes no sense to pass in a const-type as its arg. A template can't be const or non-const, only types can be const or non-const. What does const B
even mean?
const int
makes sense. const vector<int>
makes sense, as does vector<const int>
. But what would const vector
mean?
(Pinch-of-salt warning: I wasn't even aware of template-template-classes before seeing this question.)
To make this more concrete, imagine B and C are:
template<class T>
struct B
{
T t;
};
template<template<class> class T1, class T2>
struct C
{
T2 t2;
T1<T2> t1;
};
c2 will be of type
C<B,const double>
==> struct { const double t2; T1<const double> t1;}
==> struct { const double t2; struct { const double b; } t1;}
What would you expect c3 to be? That t1 would itself be const, while t1.b is non-const? I suppose that makes sense.
This exact code does compile in VS2010. I don't know you compiler but I suggest you to check in the compiler developer's bug database if a bug like that isn't registered.
I'll try it in GCC see.
Ok GCC(4.5.1) does gives the error. I guess we'll have to wait for someone with standard knowledge to know if it's standard behaviour or a bug.
CLang (2.8) does gives the same error (with exactly the same message).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With