• <bdo id='MrKGe'></bdo><ul id='MrKGe'></ul>
    <tfoot id='MrKGe'></tfoot>

  • <i id='MrKGe'><tr id='MrKGe'><dt id='MrKGe'><q id='MrKGe'><span id='MrKGe'><b id='MrKGe'><form id='MrKGe'><ins id='MrKGe'></ins><ul id='MrKGe'></ul><sub id='MrKGe'></sub></form><legend id='MrKGe'></legend><bdo id='MrKGe'><pre id='MrKGe'><center id='MrKGe'></center></pre></bdo></b><th id='MrKGe'></th></span></q></dt></tr></i><div id='MrKGe'><tfoot id='MrKGe'></tfoot><dl id='MrKGe'><fieldset id='MrKGe'></fieldset></dl></div>
    1. <small id='MrKGe'></small><noframes id='MrKGe'>

        <legend id='MrKGe'><style id='MrKGe'><dir id='MrKGe'><q id='MrKGe'></q></dir></style></legend>

        为什么编译器会在模板参数列表中选择基类构造函数?

        时间:2023-10-17
          <bdo id='Q9o79'></bdo><ul id='Q9o79'></ul>

          <small id='Q9o79'></small><noframes id='Q9o79'>

            <i id='Q9o79'><tr id='Q9o79'><dt id='Q9o79'><q id='Q9o79'><span id='Q9o79'><b id='Q9o79'><form id='Q9o79'><ins id='Q9o79'></ins><ul id='Q9o79'></ul><sub id='Q9o79'></sub></form><legend id='Q9o79'></legend><bdo id='Q9o79'><pre id='Q9o79'><center id='Q9o79'></center></pre></bdo></b><th id='Q9o79'></th></span></q></dt></tr></i><div id='Q9o79'><tfoot id='Q9o79'></tfoot><dl id='Q9o79'><fieldset id='Q9o79'></fieldset></dl></div>
              <tbody id='Q9o79'></tbody>

                1. <tfoot id='Q9o79'></tfoot>
                2. <legend id='Q9o79'><style id='Q9o79'><dir id='Q9o79'><q id='Q9o79'></q></dir></style></legend>
                3. 本文介绍了为什么编译器会在模板参数列表中选择基类构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  这个问题的后续问题.

                  基本上,在下面的代码中,为什么编译器认为C的构造函数中A里面的B是指B 基类的(不可访问的)构造函数?

                  Basically, in the following code, why does the compiler think that the B inside A<B> in Cs constructor refer to the (inaccessible) constructor of the B base class?

                  struct B{};
                  
                  template <typename T>
                  struct A : private T{};
                  
                  struct C : public A<B>{                                                                             
                      C(A<B>);   // ERROR HERE
                  };
                  

                  Ideone 上的实例. 输出:

                  prog.cpp:1:9: 错误:'struct B B::B' 不可访问
                  prog.cpp:7:7: 错误:在此上下文中

                  prog.cpp:1:9: error: 'struct B B::B' is inaccessible
                  prog.cpp:7:7: error: within this context

                  请注意,如果您将构造函数参数更改为 A<B*>A<B&> 甚至 A<;const B>.还要注意 MSVC10、GCC 4.7 和 Clang 3.1 ToT 中的三个会出错,所以它必须是 C++ 规范中的内容.是什么?

                  Note that the same error pops up if you change the constructor argument to A<B*>, A<B&> or even A<const B>. Also note that three of MSVC10, GCC 4.7 and Clang 3.1 ToT will error out, so it must be something in the C++ spec. What is it?

                  推荐答案

                  该标准允许注入的类名比原始名称更难访问.甚至在 §11.1/5 的注释中也提到了这一点,并附有示例:

                  The standard allows injected class names to be less accessible than the original names. This is even mentioned in a note in §11.1/5, together with an example:

                  [ 注意: 在派生类中,查找基类名称会在中找到注入的类名而不是基类的名称声明它的范围.注入的类名可能是比其作用域中的基类名称更难访问它被宣布.—结束注释 ]

                  [ Note: In a derived class, the lookup of a base class name will find the injected-class-name instead of the name of the base class in the scope in which it was declared. The injected-class-name might be less accessible than the name of the base class in the scope in which it was declared. —end note ]

                  [ 示例:

                  class A { };
                  class B : private A { };
                  class C : public B {
                    A *p; // error: injected-class-name A is inaccessible
                    ::A *q; // OK
                  };
                  

                  结束示例 ]

                  访问 A 不合格使用注入的名称,该名称不可访问,因为它来自私有继承.访问 A 限定使用声明的名称,该名称可在全局范围内访问.

                  Accessing A unqualified uses the injected name, which is not accessible because it comes from private inheritance. Accessing A qualified uses the declared name, which is accessible in the global scope.

                  这篇关于为什么编译器会在模板参数列表中选择基类构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                    <bdo id='3OLG1'></bdo><ul id='3OLG1'></ul>
                    <legend id='3OLG1'><style id='3OLG1'><dir id='3OLG1'><q id='3OLG1'></q></dir></style></legend>

                      1. <small id='3OLG1'></small><noframes id='3OLG1'>

                      2. <tfoot id='3OLG1'></tfoot>
                      3. <i id='3OLG1'><tr id='3OLG1'><dt id='3OLG1'><q id='3OLG1'><span id='3OLG1'><b id='3OLG1'><form id='3OLG1'><ins id='3OLG1'></ins><ul id='3OLG1'></ul><sub id='3OLG1'></sub></form><legend id='3OLG1'></legend><bdo id='3OLG1'><pre id='3OLG1'><center id='3OLG1'></center></pre></bdo></b><th id='3OLG1'></th></span></q></dt></tr></i><div id='3OLG1'><tfoot id='3OLG1'></tfoot><dl id='3OLG1'><fieldset id='3OLG1'></fieldset></dl></div>
                            <tbody id='3OLG1'></tbody>