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

為什么指向 int 的指針轉換為 void* 而指向函數的

Why does pointer to int convert to void* but pointer to function convert to bool?(為什么指向 int 的指針轉換為 void* 而指向函數的指針轉換為 bool?)
本文介紹了為什么指向 int 的指針轉換為 void* 而指向函數的指針轉換為 bool?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

C++ 草案標準 (N3337) 有以下關于指針轉換的內容:

<塊引用>

4.10 指針轉換

2 類型為指向 cv T 的指針"的右值,其中 T 是對象類型,可以轉換為輸入指向 cv void 的指針".將指向 cv T 的指針"轉換為指向 cv void 的指針"的結果指向T 類型的對象所在的存儲位置的開始,就好像該對象是 T 類型的最派生對象 (1.8)(即,不是基類)子對象).

<塊引用>

4.12 布爾轉換

1 算術、枚舉、指針或指向成員類型的指針的右值可以轉換為 bool 類型的右值.零值、空指針值或空成員指針值轉換為假;任何其他值都轉換為 true

基于上述,將函數指針或指向 int 的指針轉換為 void* 以及 bool.

但是,如果兩者都可以選擇,指針應該轉換為哪一個?

然后,為什么指向函數的指針會轉換為 bool 而指向 int 的指針會轉換為 void*?

程序:

#include 使用命名空間標準;void foo(const void* ptr){std::cout <<在 foo(void*)"中<<std::endl;}void foo(bool b){std::cout <<在 foo(bool)"中<<std::endl;}空欄(){}int main(){int i = 0;foo(&bar);foo(&i);返回0;}

輸出,使用 g++ 4.7.3:

<前>在 foo(bool)在 foo(void*)

解決方案

基于上述,將函數指針或指向 int 的指針轉換為 void* 以及 bool.

引用說明指向對象的指針可以轉換為cv void *.函數不是對象,這取消了轉換為 cv void * 的資格,只留下 bool.

<小時><塊引用>

但是,如果兩者都可以選擇,指針應該轉換為哪一個?

它應該通過 bool 轉換為 const void *.為什么?好吧,準備開始重載解決方案 (§13.3 [over.match]/2) 的旅程.當然,強調我的.

<塊引用>

但是,一旦確定了候選函數和參數列表,在所有情況下最佳函數的選擇都是相同的:

——首先,候選函數的一個子集(那些具有適當數量的參數并滿足某些其他條件)被選擇以形成一組可行的功能(13.3.2).

——然后??根據將每個參數與每個可行函數的相應參數匹配所需的隱式轉換序列(13.3.3.1)選擇最佳可行函數.

那么這些隱式轉換序列呢?

讓我們跳到 §13.3.3.1 [over.best.ics]/3 看看隱式轉換序列是什么:

<塊引用>

格式良好的隱式轉換序列是以下形式之一:
— 標準轉換序列 (13.3.3.1.1),
— 用戶定義的轉換序列 (13.3.3.1.2),或
— 省略號轉換序列(13.3.3.1.3).

我們對標準轉換序列感興趣.讓我們跳到標準轉換序列(第 13.3.3.1.1 節 [over.ics.scs]):

<塊引用>

1 表 12 總結了第 4 條中定義的轉換,并將它們劃分為四個不相交的類別:左值轉換、資格調整、提升和轉換.[注意:這些類別在值類別、cv 限定和數據表示方面是正交的:左值轉換不會改變類型的 cv 限定或數據表示;資格調整不會改變類型的價值類別或數據表示;并且促銷和轉換不會更改類型的值類別或 cv 限定.— 尾注 ]

2 [注:如第 4 條所述,標準轉換序列要么是身份轉換本身(即不轉換),要么由其他四個類別的一到三個轉換組成.

重要的部分在/2.標準轉換序列可以是單個標準轉換.這些標準轉換列于表 12 中,如下所示.請注意,您的指針轉換和布爾轉換都在其中.

從這里,我們學到了一些重要的東西:指針轉換和布爾轉換具有相同的等級.請記住,當我們開始對隱式轉換序列進行排名時(第 13.3.3.2 節 [over.ics.rank]).

查看/4,我們看到:

<塊引用>

標準轉換序列按其等級排序:完全匹配比促銷更好的轉換,而促銷比轉換更好.除非以下規則之一適用,否則具有相同等級的兩個轉換序列是不可區分的:

——不將指針、指向成員的指針或 std::nullptr_t 轉換為 bool 的轉換是比那個好.

我們以非常明確的聲明形式找到了答案.萬歲!

The C++ Draft Standard (N3337) has the following about conversion of pointers:

4.10 Pointer conversions

2 An rvalue of type "pointer to cv T," where T is an object type, can be converted to an rvalue of type "pointer to cv void." The result of converting a "pointer to cv T" to a "pointer to cv void" points to the start of the storage location where the object of type T resides, as if the object is a most derived object (1.8) of type T (that is, not a base class subobject).

and

4.12 Boolean conversions

1 An rvalue of arithmetic, enumeration, pointer, or pointer to member type can be converted to an rvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false; any other value is converted to true

Based on the above, it is perfectly OK to convert a function pointer or a pointer to an int to a void* as well as bool.

However, given the choice of both, which one should a pointer convert to?

And then, why does a pointer to a function convert to a bool and a pointer to an int convert to a void*?

Program:

#include <iostream>
using namespace std;

void foo(const void* ptr)
{
   std::cout << "In foo(void*)" << std::endl;
}

void foo(bool b)
{
   std::cout << "In foo(bool)" << std::endl;
}

void bar()
{
}

int main()
{
   int i = 0;
   foo(&bar);
   foo(&i);
   return 0;
}

Output, using g++ 4.7.3:

In foo(bool)
In foo(void*)

解決方案

Based on the above, it is perfectly OK to convert a function pointer or a pointer to an int to a void* as well as bool.

The quotation states that a pointer to an object can be converted to cv void *. Functions are not objects, and this disqualifies the conversion to cv void *, leaving only bool.


However, given the choice of both, which one should a pointer convert to?

It should convert to const void * over bool. Why? Well, prepare for a journey that starts in Overload Resolution (§13.3 [over.match]/2). Emphasis mine, of course.

But, once the candidate functions and argument lists have been identified, the selection of the best function is the same in all cases:

— First, a subset of the candidate functions (those that have the proper number of arguments and meet certain other conditions) is selected to form a set of viable functions (13.3.2).

— Then the best viable function is selected based on the implicit conversion sequences (13.3.3.1) needed to match each argument to the corresponding parameter of each viable function.

So what about these implicit conversion sequences?

Let's jump over to §13.3.3.1 [over.best.ics]/3 and see just what an implicit conversion sequence is:

A well-formed implicit conversion sequence is one of the following forms:
— a standard conversion sequence (13.3.3.1.1),
— a user-defined conversion sequence (13.3.3.1.2), or
— an ellipsis conversion sequence (13.3.3.1.3).

We're interested in standard conversions sequences. Let's pop over to Standard Conversion Sequences (§13.3.3.1.1 [over.ics.scs]):

1 Table 12 summarizes the conversions defined in Clause 4 and partitions them into four disjoint categories: Lvalue Transformation, Qualification Adjustment, Promotion, and Conversion. [ Note: These categories are orthogonal with respect to value category, cv-qualification, and data representation: the Lvalue Transformations do not change the cv-qualification or data representation of the type; the Qualification Adjustments do not change the value category or data representation of the type; and the Promotions and Conversions do not change the value category or cv-qualification of the type. — end note ]

2 [ Note: As described in Clause 4, a standard conversion sequence is either the Identity conversion by itself (that is, no conversion) or consists of one to three conversions from the other four categories.

The important part is in /2. A standard conversion sequence is allowed to be a single standard conversion. These standard conversions are listed in Table 12, shown below. Notice that both your Pointer Conversions and Boolean Conversions are in there.

From here, we learn something important: Pointer conversions and boolean conversions have the same rank. Remember that as we head to Ranking Implicit Conversion Sequences (§13.3.3.2 [over.ics.rank]).

Looking at /4, we see:

Standard conversion sequences are ordered by their ranks: an Exact Match is a better conversion than a Promotion, which is a better conversion than a Conversion. Two conversion sequences with the same rank are indistinguishable unless one of the following rules applies:

— A conversion that does not convert a pointer, a pointer to member, or std::nullptr_t to bool is better than one that does.

We've found our answer in the form of a very explicit statement. Hooray!

這篇關于為什么指向 int 的指針轉換為 void* 而指向函數的指針轉換為 bool?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 亚洲成人观看 | 99精品国产一区二区青青牛奶 | 国产精品免费一区二区三区四区 | 欧美日韩在线免费观看 | 欧美激情久久久 | 中文一区 | 欧美一区二 | 成人片网址 | 国内精品成人 | 国产96色在线 | 国产在线中文字幕 | 午夜久久久久久久久久一区二区 | 婷婷激情综合 | 成人免费观看男女羞羞视频 | 久久成人精品视频 | 在线观看国产www | 色视频www在线播放国产人成 | 精品欧美一区二区三区久久久 | 国产在线视频99 | 黄色毛片在线观看 | 天天躁日日躁狠狠的躁天龙影院 | 91免费视频观看 | 欧美日本一区二区 | 久草在线 | 蜜桃免费一区二区三区 | 国产在线播放av | 91天堂| 国产精品毛片一区二区在线看 | 日本一区二区高清视频 | 免费簧片视频 | 爱爱综合网 | 91高清视频在线观看 | 日韩欧美视频在线 | 中文区中文字幕免费看 | 成人精品毛片 | a毛片| 亚洲国产网址 | 成年网站在线观看 | 中文字幕在线观看一区 | 日韩精品在线播放 | 国产欧美精品区一区二区三区 |