是指代指针正交的参考文献

Are references orthogonal to pointers?

本文关键字:参考文献 指针      更新时间:2023-10-16

这个问题是将语言正交的(如缺乏冗余(特征。C 中的参考文献(Never Null [1](可以用指针实现,同时编译器检查语义差异。像Java或C#这样的语言只有参考文献(也许是null(,但没有指针,而C则相反。因此,一个人足以满足完整语言。C 中的行为是否足够与正交?

[1]您可以退还nullptr,但这是未定义的。

由于参考折叠规则(§8.3.2(,参考已成为非常有用;他们现在支持"完美的转发"事物而不是可以用指针。示例:

template<typename T>
class some_container
{
public:
    // ... lots of other stuff
    template<typename... Args>
    void emplace_back(Args&&... args)
    {
        T* ptr = find_slot_for_new_data();
        new (ptr) T( std::forward<Args>(args)... );
    }
};

这将构建一个 T在场,(emplace_xxx是委派构造函数(它将使用
"完美的转发"来做到这一点,也就是说

T&&推导T将保留:

  • 类型
  • 价值类别
  • cv-qualifiers

这是指指针。

引用不再是C 中更严格的指针,因为您可以通过获取地址"转换"指针的引用。这丝毫不说它们是多余的,但它们与正交相反。他们本质上做同样的事情。

这是指针和引用的差异。

#include <vector>
int main() {
   // Compiles and it contains pointers to ints 
   std::vector<int*> vpInts; 
   return 0;
}

vers

#include <vector>
int main() {
    // Does not compile
    std::vector<int&> vrInts;
    return 0;
}

第二种情况是非法的,这在MSVS2017 CE的MSVS的编译器错误中特别显示。

1>------ Build started: Project: Test1z, Configuration: Debug Win32 ------
1>main.cpp
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(774): error C2528: 'pointer': pointer to reference is illegal
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(984): note: see reference to class template instantiation 'std::allocator<_Ty>' being compiled
1>        with
1>        [
1>            _Ty=int &
1>        ]
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includevector(441): note: see reference to class template instantiation 'std::_Wrap_alloc<std::allocator<_Ty>>' being compiled
1>        with
1>        [
1>            _Ty=int &
1>        ]
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includevector(493): note: see reference to class template instantiation 'std::_Vec_base_types<_Ty,_Alloc>' being compiled
1>        with
1>        [
1>            _Ty=int &,
1>            _Alloc=std::allocator<int &>
1>        ]
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includevector(701): note: see reference to class template instantiation 'std::_Vector_alloc<std::_Vec_base_types<_Ty,_Alloc>>' being compiled
1>        with
1>        [
1>            _Ty=int &,
1>            _Alloc=std::allocator<int &>
1>        ]
1>c:usersskilz80documentsvisual studio 2017projectstest1ztest1zmain.cpp(15): note: see reference to class template instantiation 'std::vector<int &,std::allocator<_Ty>>' being compiled
1>        with
1>        [
1>            _Ty=int &
1>        ]
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(775): error C2528: 'const_pointer': pointer to reference is illegal
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(798): error C2535: 'int &(*std::allocator<_Ty>::address(int &) noexcept const)': member function already defined or declared
1>        with
1>        [
1>            _Ty=int &
1>        ]
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(792): note: see declaration of 'std::allocator<_Ty>::address'
1>        with
1>        [
1>            _Ty=int &
1>        ]
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(918): error C2528: 'pointer': pointer to reference is illegal
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(988): note: see reference to class template instantiation 'std::allocator_traits<_Alloc>' being compiled
1>        with
1>        [
1>            _Alloc=std::allocator<int &>
1>        ]
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(919): error C2528: 'const_pointer': pointer to reference is illegal
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(938): error C2528: '<template-parameter>': pointer to reference is illegal
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(944): error C2528: '<template-parameter>': pointer to reference is illegal
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(1031): error C2535: 'int &(*std::_Wrap_alloc<std::allocator<_Ty>>::address(int &) const)': member function already defined or declared
1>        with
1>        [
1>            _Ty=int &
1>        ]
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(1025): note: see declaration of 'std::_Wrap_alloc<std::allocator<_Ty>>::address'
1>        with
1>        [
1>            _Ty=int &
1>        ]
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(159): error C2528: 'abstract declarator': pointer to reference is illegal
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includevector(444): note: see reference to class template instantiation 'std::_Is_simple_alloc<std::_Wrap_alloc<std::allocator<_Ty>>>' being compiled
1>        with
1>        [
1>            _Ty=int &
1>        ]
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(159): error C2528: '<template-parameter>': pointer to reference is illegal
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(161): error C2528: 'abstract declarator': pointer to reference is illegal
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(161): error C2528: '<template-parameter>': pointer to reference is illegal
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includevector(452): error C2528: '<template-parameter>': pointer to reference is illegal
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(176): error C2528: 'pointer': pointer to reference is illegal
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includevector(497): note: see reference to class template instantiation 'std::_Simple_types<int &>' being compiled
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includexmemory0(177): error C2528: 'const_pointer': pointer to reference is illegal
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includevector(951): error C2535: 'void std::vector<int &,std::allocator<_Ty>>::push_back(_Ty)': member function already defined or declared
1>        with
1>        [
1>            _Ty=int &
1>        ]
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includevector(945): note: see declaration of 'std::vector<int &,std::allocator<_Ty>>::push_back'
1>        with
1>        [
1>            _Ty=int &
1>        ]
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includevector(1033): error C2535: 'std::_Vector_iterator<std::_Vector_val<std::_Simple_types<int &>>> std::vector<int &,std::allocator<_Ty>>::insert(std::_Vector_const_iterator<std::_Vector_val<std::_Simple_types<int &>>>,_Ty)': member function already defined or declared
1>        with
1>        [
1>            _Ty=int &
1>        ]
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includevector(1027): note: see declaration of 'std::vector<int &,std::allocator<_Ty>>::insert'
1>        with
1>        [
1>            _Ty=int &
1>        ]
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includevector(1634): error C2528: 'data': pointer to reference is illegal
1>c:program files (x86)microsoft visual studio2017communityvctoolsmsvc14.10.25017includevector(1639): error C2528: 'data': pointer to reference is illegal
1>Done building project "Test1z.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

基本上,在C 中,指针本身是一个已声明的变量,在堆栈或堆上占用内存,其中引用可能或不可以引用它引用已创建的对象或已在存储器中的变量。这取决于编译器将如何对待它;一些编译器将通过使用移动语义来通过向上和弹出寄存器进行移动语义来优化内存足迹,而指针变量将始终占用指定的内存量。有相似性使指针确实指向另一个变量或对象的内存地址,并且引用引用另一个变量或对象的已创建实例的内存地址。