Tom*_*Tom 8 c++ templates const
我不明白为什么这不编译:
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
}
Run Code Online (Sandbox Code Playgroud)
(我发现标准的参考有点神秘)
我需要更改什么才能编译?
编辑:
编译器细节(似乎是相关的):
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)
Run Code Online (Sandbox Code Playgroud)
EDIT2:
通过解释,我试图做这样的事情:
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;
Run Code Online (Sandbox Code Playgroud)
非模板化容器的技术可在此提升文档的末尾找到:http://www.boost.org/doc/libs/1_46_1/libs/iterator/doc/iterator_facade.html
我想解决方案不是嵌套模板.回想起来,这似乎是显而易见的.
C 的第一个参数不是类型,因此传递 const 类型作为其参数是没有意义的。模板不能是 const 或非常量,只有类型可以是 const 或非常量。甚至是什么const B意思?
const int说得通。const vector<int>有道理,就像 一样vector<const int>。但这const vector意味着什么呢?
(少许盐警告:在看到这个问题之前,我什至不知道模板模板类。)
为了更具体,假设 B 和 C 是:
template<class T>
struct B
{
T t;
};
template<template<class> class T1, class T2>
struct C
{
T2 t2;
T1<T2> t1;
};
Run Code Online (Sandbox Code Playgroud)
c2 将是类型
C<B,const double>
==> struct { const double t2; T1<const double> t1;}
==> struct { const double t2; struct { const double b; } t1;}
Run Code Online (Sandbox Code Playgroud)
您期望 c3 是什么?t1 本身是 const,而 t1.b 是非 const?我想这是有道理的。
| 归档时间: |
|
| 查看次数: |
2682 次 |
| 最近记录: |