jjzjj

c++ - 嵌套类声明 : template vs non-template outer class

coder 2024-02-19 原文

我有一个 C++ 模板类,里面有一个嵌套类,比如:

template<int d>
class Outer_t
{
public:
    class Inner;

    Inner i;
};

template<int d>
class Outer_t<d>::Inner
{
public:
    float x;
};

int main ()
{
    Outer_t<3> o_t; // 3 or any arbitrary int
    o_t.i.x = 1.0;

  return 0;
}

编译没有任何问题。然而,一旦我声明了一个类似的非模板类,就像这样:

class Outer_1
{
public:
    class Inner;

    Inner i;
};

class Outer_1::Inner
{
public:
    float x;
};

int main ()
{
    Outer_1 o1;
    o1.i.x = 1.0;

  return 0;
}

我开始收到以下错误(我使用的是 gcc 4.6.3):“错误:字段‘i’的类型不完整”。我知道我可以通过简单地在外部类中内联定义内部类来解决这个问题:

class Outer_2
{
public:
    class Inner {
    public:
        float x;
    };

    Inner i;
};

这会编译,但我想避免内联定义嵌套类。所以我有两个问题:模板和非模板嵌套类声明之间出现这种明显奇怪差异的原因是什么?是否有一种优雅的方式在外部类中声明和使用嵌套类,同时避免内联定义它,与模板类的风格非常相似?预先感谢您的帮助!

最佳答案

为什么会编译出错?

编译器需要知道成员变量类型的大小才能进行对象布局。对于 Outer_1,因为 Inner 只是一个前向声明,我们不知道它的大小。因此编译错误。然而,对于 Outer_2,定义是内联的,所以当我们到达 Inner 类型的成员变量时,我们知道它的大小。

为什么模板能通过?

另一方面,类模板在模板实例化发生之前不会被定义。在您的示例中,隐式 实例化发生在main 中。到那时,Inner 的定义就完成了,因此它的大小是已知的。在定义Inner之前使用explicit实例化可以看出是这样的。

 template<int d>
 class Outer_t
 {
 public:
     class Inner;

     Inner i;
 };

 template class Outer_t<3>;  // explicit instantation

 template<int d>
 class Outer_t<d>::Inner
 {
 public:
     float x;
 };

 int main ()
 {
     Outer_t<3> o_t; // 3 or any arbitrary int
     o_t.i.x = 1.0;

   return 0;
 }

Clang 产生以下错误:

 a.cc:7:11: error: implicit instantiation of undefined member 'Outer_t<3>::Inner'
     Inner i;
           ^
 a.cc:10:16: note: in instantiation of template class 'Outer_t<3>' requested here
 template class Outer_t<3>;
           ^
 a.cc:5:11: note: member is declared here
     class Inner;
           ^

解决方案

如果您想提取嵌套类的定义,我建议的解决方案是像 Outer_t 一样对其进行模板化,但为方便起见提供一个别名。

 template <typename Dummy = void>
 class Outer_1_Impl {
   public:

   class Inner;

   Inner i;
 };

 template <typename Dummy>
 class Outer_1_Impl<Dummy>::Inner {
   public:
   float x;
 };

 using Outer_1 = Outer_1_Impl<>;

 int main () {
   Outer_1 o1;
   o1.i.x = 1.0;
 }

关于c++ - 嵌套类声明 : template vs non-template outer class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23963804/

有关c++ - 嵌套类声明 : template vs non-template outer class的更多相关文章

  1. ruby-on-rails - Rails 编辑表单不显示嵌套项 - 2

    我得到了一个包含嵌套链接的表单。编辑时链接字段为空的问题。这是我的表格:Editingkategori{:action=>'update',:id=>@konkurrancer.id})do|f|%>'Trackingurl',:style=>'width:500;'%>'Editkonkurrence'%>|我的konkurrencer模型:has_one:link我的链接模型:classLink我的konkurrancer编辑操作:defedit@konkurrancer=Konkurrancer.find(params[:id])@konkurrancer.link_attrib

  2. ruby - 将散列转换为嵌套散列 - 2

    这道题是thisquestion的逆题.给定一个散列,每个键都有一个数组,例如{[:a,:b,:c]=>1,[:a,:b,:d]=>2,[:a,:e]=>3,[:f]=>4,}将其转换为嵌套哈希的最佳方法是什么{:a=>{:b=>{:c=>1,:d=>2},:e=>3,},:f=>4,} 最佳答案 这是一个迭代的解决方案,递归的解决方案留给读者作为练习:defconvert(h={})ret={}h.eachdo|k,v|node=retk[0..-2].each{|x|node[x]||={};node=node[x]}node[

  3. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  4. ruby-on-rails - active_admin 目录中的常量警告重新声明 - 2

    我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA

  5. Ruby——嵌套类和子类是一回事吗? - 2

    下面例子中的Nested和Child有什么区别?是否只是同一事物的不同语法?classParentclassNested...endendclassChild 最佳答案 不,它们是不同的。嵌套:Computer之外的“Processor”类只能作为Computer::Processor访问。嵌套为内部类(namespace)提供上下文。对于ruby​​解释器Computer和Computer::Processor只是两个独立的类。classComputerclassProcessor#Tocreateanobjectforthisc

  6. ruby - 模块嵌套代码风格偏好 - 2

    我的假设是moduleAmoduleBendend和moduleA::Bend是一样的。我能够从thisblog找到解决方案,thisSOthread和andthisSOthread.为什么以及什么时候应该更喜欢紧凑语法A::B而不是另一个,因为它显然有一个缺点?我有一种直觉,它可能与性能有关,因为在更多命名空间中查找常量需要更多计算。但是我无法通过对普通类进行基准测试来验证这一点。 最佳答案 这两种写作方法经常被混淆。首先要说的是,据我所知,没有可衡量的性能差异。(在下面的书面示例中不断查找)最明显的区别,可能也是最著名的,是你的

  7. ruby-on-rails - 使用回形针的嵌套形式 - 2

    我有一个名为posts的模型,它有很多附件。附件模型使用回形针。我制作了一个用于创建附件的独立模型,效果很好,这是此处说明的View(https://github.com/thoughtbot/paperclip):@attachment,:html=>{:multipart=>true}do|form|%>posts中的嵌套表单如下所示:prohibitedthispostfrombeingsaved:@attachment,:html=>{:multipart=>true}do|at_form|%>附件记录已创建,但它是空的。文件未上传。同时,帖子已成功创建...有什么想法吗?

  8. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  9. ruby - 使用 `+=` 和 `send` 方法 - 2

    如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:

  10. ruby - 如何计算 Liquid 中的变量 +1 - 2

    我对如何计算通过{%assignvar=0%}赋值的变量加一完全感到困惑。这应该是最简单的任务。到目前为止,这是我尝试过的:{%assignamount=0%}{%forvariantinproduct.variants%}{%assignamount=amount+1%}{%endfor%}Amount:{{amount}}结果总是0。也许我忽略了一些明显的东西。也许有更好的方法。我想要存档的只是获取运行的迭代次数。 最佳答案 因为{{incrementamount}}将输出您的变量值并且不会影响{%assign%}定义的变量,我

随机推荐