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

Constexpr 指針值

Constexpr pointer value(Constexpr 指針值)
本文介紹了Constexpr 指針值的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我試圖聲明一個 constexpr 指針初始化為某個常量整數值,但 clang 挫敗了我的所有嘗試:

I am trying to declare a constexpr pointer initialized to some constant integer value, but clang is foiling all my attempts:

嘗試 1:

constexpr int* x = reinterpret_cast<int*>(0xFF);

test.cpp:1:20: note: reinterpret_cast is not allowed in a constant expression

嘗試 2:

constexpr int* x = (int*)0xFF;

test.cpp:1:20: note: cast which performs the conversions of a reinterpret_cast is not allowed in a constant expression

嘗試 3:

constexpr int* x = (int*)0 + 0xFF;

test.cpp:1:28: note: cannot perform pointer arithmetic on null pointer

設計不允許我嘗試這樣做嗎?如果是這樣,為什么?如果沒有,我該怎么做?

Is what I'm trying to do not allowed by design? If so, why? If not, how can I do it?

注意:gcc 接受所有這些.

Note: gcc accepts all of these.

推薦答案

正如 Luc Danton 所指出的,您的嘗試被 [expr.const]/2 中的規則阻止,其中規定 core 中不允許使用各種表達式常量表達式,包括:

As Luc Danton notes, your attempts are blocked by the rules in [expr.const]/2 which say that various expressions are not allowed in core constant expressions, including:

-- 一個 reinterpret_cast
-- 具有未定義行為的操作 [注意:包括 [...] 某些指針算法 [...] -- 尾注]

-- a reinterpret_cast
-- an operation that would have undefined behavior [Note: including [...] certain pointer arithmetic [...] -- end note]

第一個項目符號排除了您的第一個示例.第二個例子被上面的第一個項目符號排除,加上來自 [expr.cast]/4 的規則:

The first bullet rules out your first example. The second example is ruled out by the first bullet above, plus the rule from [expr.cast]/4 that:

由 [...] a reinterpret_cast [...] 執行的轉換可以使用顯式類型轉換的強制轉換表示法來執行.相同的語義限制和行為適用.

The conversions performed by [...] a reinterpret_cast [...] can be performed using the cast notation of explicit type conversion. The same semantic restrictions and behaviors apply.

第二個項目是由 WG21 核心問題 1313 添加的,并闡明常量表達式中不允許對空指針進行指針運算.這排除了你的第三個例子.

The second bullet was added by WG21 core issue 1313, and clarifies that pointer arithmetic on a null pointer is not permitted in a constant expression. This rules out your third example.

即使這些限制不適用于核心常量表達式,仍然無法使用通過轉換整數產生的值來初始化 constexpr 指針,因為必須初始化 constexpr 指針變量通過一個地址常量表達式,根據[expr.const]/3,它必須計算為

Even if these restrictions did not apply to core constant expressions, it would still not be possible to initialize a constexpr pointer with a value produced by casting an integer, since a constexpr pointer variable must be initialized by an address constant expression, which, by [expr.const]/3, must evaluate to

具有靜態存儲期的對象地址、函數地址或空指針值.

the address of an object with static storage duration, the address of a function, or a null pointer value.

轉換為指針類型的整數不是這些.

An integer cast to pointer type is none of these.

g++ 還沒有嚴格執行這些規則,但它最近的版本已經越來越接近它們,所以我們應該假設它最終會完全實現它們.

g++ does not yet strictly enforce these rules, but its recent releases have been getting closer to them, so we should assume that it will eventually fully implement them.

如果您的目標是聲明一個對其執行靜態初始化的變量,您可以簡單地刪除 constexpr——clang 和 g++ 都會為這個表達式發出一個靜態初始化程序.如果出于某種原因需要將此表達式作為常量表達式的一部分,則有兩種選擇:

If your goal is to declare a variable for which static initialization is performed, you can simply drop the constexpr -- both clang and g++ will emit a static initializer for this expression. If you need this expression to be part of a constant expression for some reason, you have two choices:

  • 重構您的代碼,以便傳遞 intptr_t 而不是指針,并在需要時將其轉換為指針類型(常量表達式之外),或
  • 使用 __builtin_constant_p((int*)0xFF) 嗎?(int*)0xFF : (int*)0xFF.這種確切的表達式形式(在條件運算符的左側帶有 __builtin_constant_p)禁用了條件運算符臂中的嚴格常量表達式檢查,并且鮮為人知,但是 已記錄,gcc 都支持的非便攜式 GNU 擴展和叮當聲.
  • Restructure your code so that an intptr_t is passed around instead of a pointer, and cast it to pointer type when you need to (outside of a constant expression), or
  • Use __builtin_constant_p((int*)0xFF) ? (int*)0xFF : (int*)0xFF. This exact form of expression (with __builtin_constant_p on the left-hand-side of a conditional operator) disables strict constant expression checking in the arms of the conditional operator, and is a little-known, but documented, non-portable GNU extension supported by both gcc and clang.

這篇關于Constexpr 指針值的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 国产精品久久久久久久一区探花 | 国内精品免费久久久久软件老师 | 羞羞涩涩在线观看 | 黄色在线观看 | 久久国产三级 | 国产精品1区2区3区 男女啪啪高潮无遮挡免费动态 | 精品视频一区二区三区在线观看 | 精品欧美激情精品一区 | 日本男人天堂 | 国产一级毛片精品完整视频版 | 精品视频在线免费观看 | 久久久久成人精品免费播放动漫 | av 一区二区三区 | 久久精品综合 | 国产综合网址 | 伊人久久综合 | 国产成人精品一区二区 | 91在线精品视频 | 国产成人免费在线观看 | 天天干天天玩天天操 | 欧美一区免费 | 国产成人在线视频 | 国产精品久久久久av | 久久精品色视频 | 色成人免费网站 | 美女视频黄的 | 国产精品久久久久久久久久久久久 | 国产探花在线精品一区二区 | www.亚洲.com | 天天久久 | 日韩一区二区黄色片 | 精品一区二区三区在线视频 | 亚洲精品一二三 | 成人午夜精品 | 一区二区三区欧美在线 | 高清久久| 一区免费| 亚洲成人久久久 | 一区二区三区欧美在线 | 欧美精品一区二区三区在线播放 | 日韩欧美三级 |