简体   繁体   English

请帮助解释这些代码,也许是C ++中的“ None”?

[英]please help explain these code , maybe the 'None' in c++?

while reading the source code of folly ( https://github.com/facebook/folly/blob/master/folly/Optional.h ), I find a maybe "None" syntax implementation in c++ but cannot get the point. 在阅读folly的源代码( https://github.com/facebook/folly/blob/master/folly/Optional.h )时,我发现c ++中可能是“无”语法实现,但无法理解这一点。 The codes as follow: 代码如下:

namespace detail { struct NoneHelper {}; }
typedef int detail::NoneHelper::*None; 
const None none = nullptr;

what does these codes do? 这些代码有什么作用? Is "None" a member function pointer ? 是“无”成员函数指针吗? plz help, thanks! 请帮助,谢谢!

None is a null pointer. None是空指针。 That's quite clear. 这很清楚。 But null pointers have types, too. 但是空指针也有类型。 NoneHelper is a helper class which has no other function but to make the type of None unique. NoneHelper是一个辅助类,除了使None的类型唯一之外,它没有其他功能。

It's a pointer-to-member because that kind of pointer has limited conversions. 这是一个指向成员的指针,因为这种指针的转换次数有限。 In particular, you cannot accidentally cast a pointer-to-object to a pointer-to-member-function. 特别是,您不能意外地将指向对象的指针转换为指向成员函数的指针。

None is a typedef of a pointer-to-member of an integer type. None是指向整数类型的成员的指针的typedef。

Imagine you have a struct 想象你有一个结构

namespace detail 
{ 
    struct NoneHelper 
    { 
        int a;
        int b;
        int c; 
    }; 
};

You would be able to say: 您将能够说:

typedef int detail::NoneHelper* None; 
const None memberA = &detail::NoneHelper::a;

This is a special syntax for acquiring pointer-to-member values. 这是用于获取成员指针值的特殊语法。 It can effectively be thought of as some sort of offset: struct NoneHelper has a member variable, of type integer, at this offset from its base pointer. 可以有效地将其视为某种偏移量:结构NoneHelper在其基本指针的此偏移量处具有一个整数类型的成员变量。

You would be able to use this syntax to modify that integer without having to explicitly state which integer member you want to modify; 您将能够使用此语法来修改该整数,而不必明确声明要修改的整数成员。 For example: 例如:

void setToFive(detail::NoneHelper& object, const None ptrToMember)
{
    // .* is an operator that is used to dereference pointer-to-member variables
    object.*ptrToMember = 5;
}

detail::NoneHelper obj;
obj.a = 1;
obj.b = 2;
obj.c = 3;
None ptrToA = &detail::NoneHelper::a;  //Acquire the relative location of member a
None ptrToB = &detail::NoneHelper::b;  //Acquire the relative location of member b

setToFive(obj, ptrToA); //Sets the value of "obj.a" to 5
setToFive(obj, ptrToB); //Sets the value of "obj.b" to 5

std::cout << obj.a << obj.b << obj.c;  // should print out "553"

Edit: Please note, the "offset" idea only applies when it is a "pointer-to-member of an object". 编辑:请注意,“偏移”概念仅在它是“对象的指针到成员”时适用。 pointer-to-member syntax can be used for member functions as well ( int (SomeType::*)(int arg1) ), and (dear god hopefully) does not store an "offset" of where the member function might be contained within the object. 成员指针的语法也可以用于成员函数( int (SomeType::*)(int arg1) ),(希望亲爱的上帝)不存储成员函数可能包含在其中的“偏移量”物体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM