久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在應用間接引用時,標準是否要求指針變量的左

Does the standard mandate an lvalue-to-rvalue conversion of the pointer variable when applying indirection?(在應用間接引用時,標準是否要求指針變量的左值到右值轉換?)
本文介紹了在應用間接引用時,標準是否要求指針變量的左值到右值轉換?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

TL;DR

給定以下代碼:

int* ptr;
*ptr = 0;

在應用間接引用之前,*ptr 是否需要 lvalue-to-rvalueptr 轉換?

does *ptr require an lvalue-to-rvalue conversion of ptr before applying indirection?

該標準在很多地方涵蓋了 lvalue-to-rvalue 的主題,但似乎沒有指定足夠的信息來確定 * 運算符 是否需要這種轉換.

The standard covers the topic of lvalue-to-rvalue in many places but does not seem to specify enough information to determine whether the * operator require such a conversion.

詳情

左值到右值的轉換見N3485 在 4.1 Lvalue-to-rvalue conversion 段落 1 中說(強調我的未來):

The lvalue-to-rvalue conversion is covered in N3485 in section 4.1 Lvalue-to-rvalue conversion paragraph 1 and says (emphasis mine going forward):

可以轉換非函數、非數組類型 T 的泛左值 (3.10)到一個純右值. 53 如果 T 是一個不完整的類型,一個程序需要這種轉換是格式錯誤的.如果對象泛左值指的不是 T 類型的對象,也不是 a 類型的對象派生自 T 的類型,或者如果對象未初始化,則為程序需要進行此轉換的行為具有未定義的行為.[...]

A glvalue (3.10) of a non-function, non-array type T can be converted to a prvalue.53 If T is an incomplete type, a program that necessitates this conversion is ill-formed. If the object to which the glvalue refers is not an object of type T and is not an object of a type derived from T, or if the object is uninitialized, a program that necessitates this conversion has undefined behavior.[...]

*ptr = 0;需要這種轉換嗎?

如果我們轉到 41 它說:

If we go to section 4 paragraph 1 it says:

[...]一個標準的轉換序列將應用于一個表達式如有必要將其轉換為所需的目標類型.

[...]A standard conversion sequence will be applied to an expression if necessary to convert it to a required destination type.

那么什么時候需要?如果我們查看 5 Expressions 部分,lvalue-to-rvalue 轉換在段落 9 中提到:

So when is it necessary? If we look at section 5 Expressions the lvalue-to-rvalue conversion is mentioned in paragraph 9 which says:

每當泛左值表達式作為運算符的操作數出現時期望該操作數的純右值,即左值到右值(4.1),數組到指針 (4.2) 或函數到指針 (4.3) 標準應用轉換將表達式轉換為純右值.[...]

Whenever a glvalue expression appears as an operand of an operator that expects a prvalue for that operand, the lvalue-to-rvalue (4.1), array-to-pointer (4.2), or function-to-pointer (4.3) standard conversions are applied to convert the expression to a prvalue. [...]

和段落 11 說:

在某些情況下,表達式的出現只是為了它的副作用.這樣的表達式稱為丟棄值表達式.[...]左值到右值轉換 (4.1) 當且僅當表達式是一個 volatile 限定類型的左值,它是以下[...]

In some contexts, an expression only appears for its side effects. Such an expression is called a discarded-value expression.[...] The lvalue-to-rvalue conversion (4.1) is applied if and only if the expression is an lvalue of volatile-qualified type and it is one of the following [...]

這兩個段落似乎都不適用于此代碼示例和 5.3.1 一元運算符 段落 1 它說:

neither paragraph seems to apply to this code sample and 5.3.1 Unary operators paragraph 1 it says:

一元 * 運算符執行間接:它所指向的表達式被應用的應該是一個指向對象類型的指針,或者指向一個對象類型的指針函數類型,結果是引用對象的左值或表達式指向的函數.如果表達式的類型是指向 T 的指針",結果的類型是T".[注:間接通過指向不完整類型(cv void 除外)的指針是有效的.這樣獲得的左值可以以有限的方式使用(初始化一個參考,例如);此左值不得轉換為右值,見 4.1.——結尾說明]

The unary * operator performs indirection: the expression to which it is applied shall be a pointer to an object type, or a pointer to a function type and the result is an lvalue referring to the object or function to which the expression points. If the type of the expression is "pointer to T," the type of the result is "T." [ Note: indirection through a pointer to an incomplete type (other than cv void) is valid. The lvalue thus obtained can be used in limited ways (to initialize a reference, for example); this lvalue must not be converted to a prvalue, see 4.1. —end note ]

它似乎不需要指針的,而且我沒有看到任何指針轉換的要求,我是否遺漏了什么?

it does not seem to require the value of the pointer and I don't see any requirements for a conversion of the pointer here am I missing something?

我們為什么關心?

我在其他問題中看到了一個答案和評論,聲稱使用未初始化的指針是未定義的行為,因為需要 lvalue-to-rvalue 轉換 ptr 在應用間接之前.例如:C++ 標準究竟在哪里說取消引用一個未初始化的指針是未定義的行為? 提出了這個論點,我無法將這個論點與任何最近的標準草案版本中的布局相協調.由于我已經多次看到這個,我想得到澄清.

I have seen an answer and comments in other questions that claim the use of an uninitialized pointer is undefined behavior due the need for an lvalue-to-rvalue conversion of ptr before applying indirection. For example: Where exactly does C++ standard say dereferencing an uninitialized pointer is undefined behavior? makes this argument and I can not reconcile the argument with what is laid out in any of the recent draft versions of the standard. Since I have seen this several times I wanted to get clarification.

未定義行為的實際證據并不重要,因為正如我在上面鏈接的問題中指出的那樣,我們有其他方法可以達到未定義行為.

The actual proof of undefined behavior is not as important since as I noted in the linked question above we have others way to get to undefined behavior.

推薦答案

我已將問題中的更新部分轉換為答案,因為此時它似乎是答案,盡管我的問題無法回答令人不滿意:

I have converted the update section in my question to an answer since at this point it seems to be the answer, albeit an unsatisfactory one that my question is unanswerable:

dyp 向我指出了兩個涵蓋非常相似基礎的相關主題:

dyp pointed me to two relevant threads that cover very similar ground:

  • 什么是未指定時 C++ 運算符的操作數的值類別?
  • 初始化是否需要左值到-右值轉換?是 int x = x;優?

共識似乎是該標準不明確,因此無法提供我正在尋找的答案,約瑟夫曼斯菲爾德 發布了一份關于缺乏規范的缺陷報告,看起來仍然是打開,不清楚什么時候可以澄清.

The consensus seems to be that the standard is ill-specified and therefore can not provide the answer I am looking for, Joseph Mansfield posted a defect report on this lack of specification, and it looks like it is still open and it is not clear when it may be clarified.

對于標準的意圖,有一些常識性的論據.可以爭論 邏輯上,如果操作需要使用該操作數的值,則該操作數為純右值.另一個論點是,如果我們回顧一下 C99 草案標準說默認情況下會進行左值到右值的轉換 并注明了例外情況.C99 標準草案的相關部分是 6.3.2.1 Lvalues, arrays, and function designators 段落 2 說:

There are a few common sense arguments to be made as to the intent of the standard. One can argue Logicially, an operand is a prvalue if the operation requires using the value of that operand. Another argument is that if we look back to the C99 draft standard says an lvalue to rvalue conversion is done by default and the exceptions are noted. The relevant section from the draft C99 standard is 6.3.2.1 Lvalues, arrays, and function designators paragraph 2 which says:

除非是sizeof運算符的操作數,否則一元&運算符、++ 運算符、-- 運算符或 .運算符或賦值運算符,沒有數組類型的左值被轉換為存儲在指定對象中的值(不再是左值).[…]

Except when it is the operand of the sizeof operator, the unary & operator, the ++ operator, the -- operator, or the left operand of the . operator or an assignment operator, an lvalue that does not have array type is converted to the value stored in the designated object (and is no longer an lvalue). […]

基本上說,除了一些例外,操作數被轉換為存儲的值,并且由于間接不是例外,如果這在C++ 那么它確實可以回答我的問題.

which basically says with some exceptions an operand is converted to the value stored and since indirection is not an exception if this is clarified to also be the case in C++ as well then it would indeed make the answer to my question yes.

當我試圖澄清未定義行為的證據時,不如澄清是否強制要求進行左值到右值轉換重要.如果我們想證明未定義的行為,我們有其他方法.Jerry 的方法是一種常識性方法,其中間接要求表達式是指向對象或函數的指針,而不確定的值只會偶然指向有效對象.一般來說,C++ 標準草案沒有明確聲明說使用不確定值是未定義的,這與 C99 草案標準不同 在 C++11 和后面的標準中沒有給出明確聲明說使用不確定的值是不確定的.迭代器和擴展指針除外,我們確實有奇異值的概念,并且我們在 24.2.1 部分被告知:

As I attempted to clarify the proof of undefined behavior was less important than clarifying whether a lvalue-to-rvalue conversion is mandated. If we want to prove undefined behavior we have alternate approaches. Jerry’s approach is a common sense one and in that indirection requires that the expression be a pointer to an object or function and an indeterminate value will only by accident point to a valid object. In general the draft C++ standard does not give an explicit statement to say using an indeterminate value is undefined, unlike the C99 draft standard In C++11 and back the standard does not give an explicit statement to say using an indeterminate value is undefined. The exception being iterators and by extension pointers we do have the concept of singular value and we are told in section 24.2.1 that:

[…][ 示例:在聲明未初始化的指針 x 之后(與 int* x; 一樣),必須始終假定 x 具有指針的奇異值.—結束示例] […] 可解引用的值總是非奇異的.

[…][ Example: After the declaration of an uninitialized pointer x (as with int* x;), x must always be assumed to have a singular value of a pointer. —end example ] […] Dereferenceable values are always non-singular.

和:

無效迭代器是指可能是單數的迭代器.268

An invalid iterator is an iterator that may be singular.268

和腳注 268 說:

這個定義適用于指針,因為指針是迭代器.取消引用已失效的迭代器的效果是不確定的.

This definition applies to pointers, since pointers are iterators. The effect of dereferencing an iterator that has been invalidated is undefined.

在 C++1y ??語言發生了變化,我們確實有一個明確的聲明,使用未定義的中間值,但有一些狹隘的例外.

這篇關于在應用間接引用時,標準是否要求指針變量的左值到右值轉換?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Difference between std::reference_wrapper and simple pointer?(std::reference_wrapper 和簡單指針的區別?)
Difference between const. pointer and reference?(常量之間的區別.指針和引用?)
How to access the contents of a vector from a pointer to the vector in C++?(c++ - 如何從指向向量的指針訪問向量的內容?)
Meaning of *amp; and **amp; in C++(*amp; 的含義和**amp;在 C++ 中)
Why can#39;t I do polymorphism with normal variables?(為什么我不能對普通變量進行多態?)
Dereferencing deleted pointers always result in an Access Violation?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 精品毛片 | 黄色网址免费在线观看 | 日韩中文一区 | www.日韩 | 91在线网站 | 久久精品色欧美aⅴ一区二区 | 亚洲国产成人精品女人 | 久草www| 亚洲在线一区 | 午夜视频免费 | 免费视频二区 | 欧美精品久久久久 | 全部免费毛片在线播放网站 | 第四色影音先锋 | 国产欧美日韩一区二区三区 | 亚洲视频国产 | 久久精品中文字幕 | 神马福利 | 国产欧美日韩精品一区 | 久久99成人 | 91亚洲国产成人精品一区二三 | 国产情侣啪啪 | 欧美日韩精品中文字幕 | www.天天操.com | 国产999在线观看 | 国产 日韩 欧美 制服 另类 | 91久久综合亚洲鲁鲁五月天 | 久久久免费毛片 | 一区二区成人 | 在线亚洲欧美 | 欧美a在线 | 国产福利在线 | 午夜视频一区二区三区 | 欧美精品日韩精品 | 中文字幕在线一区二区三区 | 国产精品一区久久久 | 亚洲一区二区三区四区在线观看 | 不卡一区二区三区四区 | 中文字幕一区二区在线观看 | 午夜精品久久久久99蜜 | 久久精品国产a三级三级三级 |