是指代指针正交的参考文献
Are references orthogonal to pointers?
这个问题是将语言正交的(如缺乏冗余(特征。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 中,指针本身是一个已声明的变量,在堆栈或堆上占用内存,其中引用可能或不可以引用它引用已创建的对象或已在存储器中的变量。这取决于编译器将如何对待它;一些编译器将通过使用移动语义来通过向上和弹出寄存器进行移动语义来优化内存足迹,而指针变量将始终占用指定的内存量。有相似性使指针确实指向另一个变量或对象的内存地址,并且引用引用另一个变量或对象的已创建实例的内存地址。
相关文章:
- 我可以有一个 ELI5 作为参考和指针以及何时使用它们吗?
- 演绎指南中的参考文献和值之间的差异
- 为什么即使传递给函数作为参考,指针也未被分配
- 是指代指针正交的参考文献
- 为什么动态演员只能与参考和指针一起使用
- 参考和指针的右引用
- G 也抱怨不确定的参考文献,即使包括库
- 为什么认为参考文献在C 底漆第5个中没有变化,以下代码有效
- 库存储涉及参考文献的"equation"?
- 在指针与通过参考传递指针传递指针上使用Reference_wrapper有好处吗?
- 防止编码器错误 - 通过参考(悬挂指针)返回临时值
- 关于在ConstexPR指示和参考文献中需要固定地址的澄清
- 有关参考和指针的问题
- 访问链接的列表项目从参考这些项目的参考文献中
- CXX11不确定的参考文献6.2.0
- 为什么decltype()符合参考的指针
- 在Google测试固定装置中使用参考代替指针
- 指针与参考文献
- 关于指针和参考文献的两个简短问题
- 关于参考文献的问题和指向它们的指针