欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

C++ 利用模板偏特化和 decltype(()) 識別表達式的值類別

剛剛看到一篇 C++ 博客,里面講到用模板偏特化和 decltype() 識別值類別:lvalue glvalue xvalue rvalue prvalue。依照博客的方法試了一下,發(fā)現(xiàn)根本行不通。之后,我查閱了一下 cppreference.com 關于 decltype 關鍵字的描述,發(fā)現(xiàn)了 decltype((表達式)) 具有以下特性:

成都創(chuàng)新互聯(lián)專注于企業(yè)全網(wǎng)營銷推廣、網(wǎng)站重做改版、甘谷網(wǎng)站定制設計、自適應品牌網(wǎng)站建設、成都h5網(wǎng)站建設、商城網(wǎng)站制作、集團公司官網(wǎng)建設、外貿網(wǎng)站制作、高端網(wǎng)站制作、響應式網(wǎng)頁設計等建站業(yè)務,價格優(yōu)惠性價比高,為甘谷等各大城市提供網(wǎng)站開發(fā)制作服務。

  • 如果 表達式 的值類別是 xvaluedecltype 將會產生 T&&;
  • 如果 表達式 的值類別是 lvalue,decltype 將會產生 T&
  • 如果 表達式 的值類別是 prvalue,decltype 將會產生 T

也就是可以細分 xvaluelvalue,于是嘗試將模板偏特化和 decltype(()) 結合,發(fā)現(xiàn)這種方法可行。

#include <iostream>
#include <type_traits>

template<typename T> struct is_lvalue : std::false_type {};
template<typename T> struct is_lvalue<T&> : std::true_type {};

template<typename T> struct is_xvalue : std::false_type {};
template<typename T> struct is_xvalue<T&&> : std::true_type {};

template<typename T> struct is_glvalue : std::integral_constant<bool, is_lvalue<T>::value || is_xvalue<T>::value> {};
template<typename T> struct is_prvalue : std::integral_constant<bool, !is_glvalue<T>::value> {};
template<typename T> struct is_rvalue : std::integral_constant<bool, !is_lvalue<T>::value> {};

struct A
{
    int x = 1;
};

int main()
{
    A a;

    std::cout << std::boolalpha
    << is_lvalue<decltype(("abcd"))>::value << std::endl
    << is_glvalue<decltype(("abcd"))>::value << std::endl
    << is_xvalue<decltype(("abcd"))>::value << std::endl
    << is_rvalue<decltype(("abcd"))>::value << std::endl
    << is_prvalue<decltype(("abcd"))>::value << std::endl
    << std::endl
    << is_lvalue<decltype((a))>::value << std::endl
    << is_glvalue<decltype((a))>::value << std::endl
    << is_xvalue<decltype((a))>::value << std::endl
    << is_rvalue<decltype((a))>::value << std::endl
    << is_prvalue<decltype((a))>::value << std::endl
    << std::endl
    << is_lvalue<decltype((A()))>::value << std::endl
    << is_glvalue<decltype((A()))>::value << std::endl
    << is_xvalue<decltype((A()))>::value << std::endl
    << is_rvalue<decltype((A()))>::value << std::endl
    << is_prvalue<decltype((A()))>::value << std::endl
    << std::endl
    << is_lvalue<decltype((A().x))>::value << std::endl
    << is_glvalue<decltype((A().x))>::value << std::endl
    << is_xvalue<decltype((A().x))>::value << std::endl
    << is_rvalue<decltype((A().x))>::value << std::endl
    << is_prvalue<decltype((A().x))>::value << std::endl
    ;
}

輸出

true
true
false
false
false

true
true
false
false
false

false
false
false
true
true

false
true
true
true
false

所有的輸出結果都符合預期。

新聞標題:C++ 利用模板偏特化和 decltype(()) 識別表達式的值類別
鏈接地址:http://chinadenli.net/article2/dsogoic.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供全網(wǎng)營銷推廣、網(wǎng)站營銷服務器托管、品牌網(wǎng)站制作、微信公眾號、面包屑導航

廣告

聲明:本網(wǎng)站發(fā)布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站建設網(wǎng)站維護公司