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

C++14 變量模板:它們的目的是什么?任何使用示例

C++14 Variable Templates: what is their purpose? Any usage example?(C++14 變量模板:它們的目的是什么?任何使用示例?)
本文介紹了C++14 變量模板:它們的目的是什么?任何使用示例?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

C++14 將允許創建模板化的變量.通常的例子是一個變量 'pi',它可以被讀取以獲得各種類型的數學常數 π 的值(int 為 3;float 可能是最接近的值等)

C++14 will allow the creation of variables that are templated. The usual example is a variable 'pi' that can be read to get the value of the mathematical constant π for various types (3 for int; the closest value possible with float, etc.)

除了我們可以通過將變量包裝在模板化結構或類中來擁有此功能之外,這如何與類型轉換混合?我看到一些重疊.

Besides that we can have this feature just by wrapping a variable within a templated struct or class, how does this mix with type conversions? I see some overlapping.

除了 pi 示例之外,它如何處理非常量變量?任何使用示例來了解如何充分利用此類功能及其目的是什么?

And other than the pi example, how would it work with non-const variables? Any usage example to understand how to make the most of such a feature and what its purpose is?

推薦答案

除了 pi 示例之外,它如何與非常量一起使用變量?

And other than the pi example, how would it work with non-const variables?

目前,它似乎為類型單獨實例化變量.即,您可以將 10 分配給 n,這將與模板定義不同.

Currently, it seems to instantiate the variables separately for the type. i.e., you could assign 10 to n<int> and it would be different from the template definition.

template<typename T>
T n = T(5);

int main()
{
    n<int> = 10;
    std::cout << n<int> << " ";    // 10
    std::cout << n<double> << " "; // 5
}

如果聲明是const,則它是只讀的.如果它是一個 constexpr,就像所有 constexpr 聲明一樣,它在 constexpr(resions) 之外沒有太多用處.

If the declaration is const, it is readonly. If it's a constexpr, like all constexpr declarations, it has not much use outside constexpr(ressions).

此外我們可以通過包裝一個變量來擁有這個功能在模板化結構或類中,它如何與類型混合轉化?

Besides that we can have this feature just by wrapping a variable within a templated struct or class, how does this mix with type conversions?

這是一個簡單的提議.我無法看到它如何以重要的方式影響類型轉換.正如我已經說過的,變量的類型是您實例化模板的類型.即,decltype(n) 是 int.decltype((double)n) 是 double 等等.

It's meant to be a simple proposal. I am unable to see how it affects type conversions in a significant way. As I already stated, the type of the variable is the type you instantiated the template with. i.e., decltype(n<int>) is int. decltype((double)n<int>) is double and so on.

了解如何充分利用此類功能的任何用法示例它的目的是什么?

Any usage example to understand how to make the most of such a feature and what its purpose is?

N3651 提供了一個簡潔的理由.

N3651 provides a succinct rationale.

唉,現有的 C++ 規則不允許模板聲明聲明一個變量.對此有眾所周知的解決方法問題:

Alas, existing C++ rules do not allow a template declaration to declare a variable. There are well known workarounds for this problem:

? 使用類模板的 constexpr 靜態數據成員

? use constexpr static data members of class templates

? 使用返回所需值的 constexpr 函數模板

? use constexpr function templates returning the desired values

這些變通方法已為人所知數十年并且有據可查.標準類如 std::numeric_limits 是原型例子.盡管這些變通方法并不完美,但它們的缺點在某種程度上是可以忍受的,因為在 C++03 時代只是簡單,內置類型常量享有不受約束的直接和高效編譯時支持.所有這一切都隨著采用C++11中的constexpr變量,擴展了直接高效支持用戶定義類型的常量.現在,程序員使(類類型的)常量在程序中越來越明顯.因此,與此相關的困惑和挫折越來越大解決方法.

These workarounds have been known for decades and well documented. Standard classes such as std::numeric_limits are archetypical examples. Although these workarounds aren’t perfect, their drawbacks were tolerable to some degree because in the C++03 era only simple, builtin types constants enjoyed unfettered direct and efficient compile time support. All of that changed with the adoption of constexpr variables in C++11, which extended the direct and efficient support to constants of user-defined types. Now, programmers are making constants (of class types) more and more apparent in programs. So grow the confusion and frustrations associated with the workarounds.

...

靜態數據成員"的主要問題是:

The main problems with "static data member" are:

? 它們需要重復"聲明:一旦在類中模板,一旦在類模板之外提供真實"使用 odr 時的定義.

? they require "duplicate" declarations: once inside the class template, once outside the class template to provide the "real" definition in case the con- stants is odr-used.

? 程序員對提供兩倍相同的內容的必要性感到惱火和困惑宣言.相比之下,普通"常量聲明不需要重復聲明.

? programmers are both miffed and confused by the necessity of providing twice the same declaration. By contrast, "ordinary" constant declarations do not need duplicate declarations.

...

這個類別中眾所周知的例子可能是靜態成員numeric_limits 的函數,或諸如boost::constants::pi() 等 Constexpr 函數模板沒有遭受靜態數據成員的重復聲明"問題有;此外,它們提供功能抽象.然而,他們強制程序員提前選擇,在定義站點,如何常量將被傳遞:要么通過一個常量引用,要么通過普通的非引用類型.如果通過 const 引用傳遞,則常量必須系統地分配在靜態存儲中;如果通過非引用類型,則常量需要復制.復制不是內置類型的一個問題,但它是用戶定義的一個showstopper具有值語義的類型不僅僅是 tiny 的包裝器內置類型(例如矩陣、整數或 bigfloat 等)相比之下,普通" const(expr) 變量不受此影響問題.提供了一個簡單的定義,并決定了常量是否實際上只需要在存儲中進行布局取決于用法,而不是定義.

Well known examples in this category are probably static member functions of numeric_limits, or functions such as boost::constants::pi<T>(), etc. Constexpr functions templates do not suffer the "duplicate declarations" issue that static data members have; furthermore, they provide functional abstraction. However, they force the programmer to chose in advance, at the definition site, how the constants are to be delivered: either by a const reference, or by plain non- reference type. If delivered by const reference then the constants must be systematically be allocated in static storage; if by non-reference type, then the constants need copying. Copying isn’t an issue for builtin types, but it is a showstopper for user-defined types with value semantics that aren’t just wrappers around tiny builtin types (e.g. matrix, or integer, or bigfloat, etc.) By contrast, "ordinary" const(expr) variables do not suffer from this problem. A simple definition is provided, and the decision of whether the constants actually needs to be layout out in storage only depends on the usage, not the definition.

這篇關于C++14 變量模板:它們的目的是什么?任何使用示例?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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?(取消引用已刪除的指針總是會導致訪問沖突?)
主站蜘蛛池模板: 中文字幕在线视频精品 | 日韩二区| 91在线视频观看 | 日韩精品一区二区三区在线播放 | 成人久久18免费网站 | 久久9视频 | 亚洲av毛片 | 久久精品亚洲国产奇米99 | 天天综合成人网 | 色婷婷av777 av免费网站在线 | 一区二区三区在线观看视频 | 成人午夜精品 | 中文字幕不卡在线观看 | 国产h视频| 波多野结衣一二三区 | 美女啪啪国产 | 亚洲网站在线观看 | 亚洲成人免费 | 欧美一级片在线 | 精品在线一区 | 国产色片在线 | 91亚洲精品国偷拍自产在线观看 | 激情毛片 | 亚州毛片 | 久久日韩粉嫩一区二区三区 | 中文字幕精品一区久久久久 | 国产成人免费 | 久久久人成影片一区二区三区 | 日韩免费视频一区二区 | 我想看国产一级毛片 | 免费国产视频在线观看 | 热久久免费视频 | 欧美视频 | 日韩久久精品 | 成人综合视频在线 | 韩日av在线 | 久久久做 | 成人免费视频网站在线观看 | 日韩精品一区二区三区中文在线 | 成人一区二区三区在线 | 欧美国产日韩在线观看成人 |