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

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

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

      使用模板技巧访问私人成员

      时间:2023-05-25

          1. <legend id='sztQf'><style id='sztQf'><dir id='sztQf'><q id='sztQf'></q></dir></style></legend>
              <tbody id='sztQf'></tbody>
              <bdo id='sztQf'></bdo><ul id='sztQf'></ul>
                <tfoot id='sztQf'></tfoot>
              • <small id='sztQf'></small><noframes id='sztQf'>

                <i id='sztQf'><tr id='sztQf'><dt id='sztQf'><q id='sztQf'><span id='sztQf'><b id='sztQf'><form id='sztQf'><ins id='sztQf'></ins><ul id='sztQf'></ul><sub id='sztQf'></sub></form><legend id='sztQf'></legend><bdo id='sztQf'><pre id='sztQf'><center id='sztQf'></center></pre></bdo></b><th id='sztQf'></th></span></q></dt></tr></i><div id='sztQf'><tfoot id='sztQf'></tfoot><dl id='sztQf'><fieldset id='sztQf'></fieldset></dl></div>
                本文介绍了使用模板技巧访问私人成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                来自博客文章 访问私人成员:更安全的肮脏 by Johannes Schaub - litb:

                template<typename Tag, typename Tag::type M>
                struct Rob { 
                  friend typename Tag::type get(Tag) {
                    return M;
                  }
                };
                
                // use
                struct A {
                  A(int a):a(a) { }
                private:
                  int a;
                };
                
                // tag used to access A::a
                struct A_f { 
                  typedef int A::*type;
                  friend type get(A_f);
                };
                
                template struct Rob<A_f, &A::a>;
                
                int main() {
                  A a(42);
                  std::cout << "proof: " << a.*get(A_f()) << std::endl;
                }
                

                如何从 a 对象调用 get 函数,因为它没有在 class A 中定义?

                how get function can be call from a object since its not defined inside class A ?

                我不明白为什么 get 必须有 Tag 作为参数而不是 a.*get()=> 好的,这是由于 ADL 机制

                I don't understand why get must have Tag as parameter instead of a.*get<A_f>() => ok it's due to ADL mechanism

                推荐答案

                你不是从 a 调用 get!实际上返回的是一个指向 A 内部成员的类指针,它的类型是 int A::* 所以你需要一个 A 的实例> 访问该值.

                You are not calling get from a! Actually what get return is a class pointer to a member inside A and type of it is int A::* so you need an instance of A to access that value.

                例如让我玩一下你的代码:

                For example let me play a little with your code:

                struct A {
                    A(int a):a(a) { }
                    int b;
                private:
                    int a;
                };
                void test() {
                    auto p = &A::b;
                    std::cout << a.*p << std::endl;
                }
                

                我是否从 a 内部调用了 p ?a 没有 p,这正是你的代码中发生的事情,get 函数返回 &A::a 然后你使用 a 来读取它的值!就是这样,没有错,我认为它会在所有编译器中编译.

                Did I call p from inside a? a does not have p, this is exactly what happened in your code, get function return &A::a and you use a to read its value! that's all, nothing is wrong and I think it will be compiled in all compilers.

                这里的另一个问题是:为什么 C++ 允许使用 A 的私有成员声明模板.C++ 标准说:

                One other question here is: Why C++ allow declaring template using private member of A. C++ standard say:

                14.7.2p8 通常的访问检查规则不适用于用于指定显式实例化的名称.[注:特别是模板函数声明器中使用的参数和名称(包括参数类型、返回类型和异常规范)可能是通常无法访问的私有类型或对象模板可以是成员模板或成员函数通常无法访问.]

                14.7.2p8 The usual access checking rules do not apply to names used to specify explicit instantiations. [Note: In particular, the template arguments and names used in the function declarator (including parameter types, return types and exception specifications) may be private types or objects which would normally not be accessible and the template may be a member template or member function which would not normally be accessible.]

                但是,如果您尝试实例化或什至 typedef 指定的模板,则会出现错误.让我们稍微修改一下您的示例:

                But if you try to instantiate or even typedef specified template then you get an error. Let's modify your example slightly:

                struct A {
                private:
                    int a;
                    friend void f();
                };
                
                // Explicit instantiation - OK, no access checks
                template struct Rob<A_f, &A::a>;
                
                // Try to use the type in some way - get an error.
                struct Rob<A_f, &A::a> r;            // error
                typedef struct Rob<A_f, &A::a> R;    // error
                void g(struct Rob<A_f, &A::a>);      // error
                
                // However, it's Ok inside a friend function.
                void f() {
                    Rob<A_f, &A::a> r;               // OK
                    typedef Rob<A_f, &A::a> R;       // OK
                }
                

                这篇关于使用模板技巧访问私人成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:基于继承类的模板特化 下一篇:消除作为模板参数传递的重载成员函数指针的歧

                相关文章

                最新文章

                  <bdo id='cD4N6'></bdo><ul id='cD4N6'></ul>

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

                <i id='cD4N6'><tr id='cD4N6'><dt id='cD4N6'><q id='cD4N6'><span id='cD4N6'><b id='cD4N6'><form id='cD4N6'><ins id='cD4N6'></ins><ul id='cD4N6'></ul><sub id='cD4N6'></sub></form><legend id='cD4N6'></legend><bdo id='cD4N6'><pre id='cD4N6'><center id='cD4N6'></center></pre></bdo></b><th id='cD4N6'></th></span></q></dt></tr></i><div id='cD4N6'><tfoot id='cD4N6'></tfoot><dl id='cD4N6'><fieldset id='cD4N6'></fieldset></dl></div>
                <tfoot id='cD4N6'></tfoot>
                1. <legend id='cD4N6'><style id='cD4N6'><dir id='cD4N6'><q id='cD4N6'></q></dir></style></legend>