带有 const 模板参数的模板模板类

发布于 2024-11-07 15:31:46 字数 2143 浏览 0 评论 0原文

我不明白为什么这不能编译:(

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
}

我发现对标准的引用有点神秘)

我需要更改什么才能使其编译?

编辑:

编译器详细信息(似乎相关):

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) 

编辑2:

通过解释,我试图做这样的事情:

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;

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

我想解决方案不是嵌套模板。回想起来,这似乎是显而易见的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笨死的猪 2024-11-14 15:31:46

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

const int 是有道理的。 const vector 是有意义的,vector 也是如此。但是 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;
};

类型

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

c2 将是您期望 c3 是什么 ? t1 本身是 const,而 t1.b 是非 const?我想这是有道理的。

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.

ヅ她的身影、若隐若现 2024-11-14 15:31:46

这段代码确实可以在 VS2010 中编译。我不认识你的编译器,但我建议你检查编译器开发人员的错误数据库,如果没有注册这样的错误。

我会在 GCC 中尝试一下。


好吧,GCC(4.5.1) 确实给出了错误。我想我们必须等待具有标准知识的人才能知道这是标准行为还是错误。


CLang (2.8) 确实给出了相同的错误(具有完全相同的消息)。

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

如何视而不见 2024-11-14 15:31:46

我猜 B 不能是 const,因为此时它没有实际类型,并且编译器不知道什么是 const。除了离开 const 之外,我无法想出正确编译的解决方案,因为模板模板确实很伤脑筋。

I guess B cannot be const because at this time it has no real type and the compiler does not know what is to be const. Other than leaving the const away I cannot come up with a solution to compile correctly, as template templates are really a pain to the brain.

音栖息无 2024-11-14 15:31:46

My bet is that MSVC silently swallows the const in the same ways as the const classes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文