带有const template参数的template模板类 [英] template template class with const template parameter

查看:58
本文介绍了带有const template参数的template模板类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么它不能编译:

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

要对其进行编译,必须做些什么改变?

What do I have to change to make it compile?

编辑:

编译器详细信息(似乎是无关紧要的):

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;

在此增强文档的末尾可以找到非模板容器的技术: http://www.boost.org/doc/libs/1_46_1/libs /iterator/doc/iterator_facade.html

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.

推荐答案

C的第一个参数不是类型,因此传入没有任何意义。 const类型为其arg。模板不能是const或非const,只有类型可以是const或非const。 const B 到底是什么意思?

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 说得通。 const vector< int> vector< const int> 一样有意义。但是常量向量是什么意思?

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

为了更具体一点,假设B和C为:

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的类型为

C<B,const double>   
==>   struct { const double t2; T1<const double> t1;}
==> struct { const double t2; struct { const double b; } t1;}

您期望c3是什么? t1本身就是常量,而t1.b是非常量吗?我想这是有道理的。

What would you expect c3 to be? That t1 would itself be const, while t1.b is non-const? I suppose that makes sense.

这篇关于带有const template参数的template模板类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆