拨开荷叶行,寻梦已然成。仙女莲花里,翩翩白鹭情。
IMG-LOGO
主页 文章列表 C++ 利用模板偏特化和 decltype(()) 识别表达式的值类别

C++ 利用模板偏特化和 decltype(()) 识别表达式的值类别

白鹭 - 2022-02-11 1984 0 0

刚刚看到一篇 C++ 博客,里面讲到用模板偏特化和 decltype() 识别值类别:lvalue glvalue xvalue rvalue prvalue,依照博客的方法试了一下,发现根本行不通,之后,我查阅了一下 cppreference.com 关于 decltype 关键字的描述,发现了 decltype((表达式)) 具有以下特性:

  • 如果 表达式 的值类别是亡值,decltype 将会产生 T&&
  • 如果 表达式 的值类别是左值,decltype 将会产生 T&
  • 如果 表达式 的值类别是纯右值,decltype 将会产生 T

也就是可以细分 xvaluelvalue,于是尝试将模板偏特化和 decltype(()) 结合,发现这种方法可行,

#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

本文来自博客园,作者:mkckr0,转载请注明原文链接:https://www.cnblogs.com/mkckr0/p/15820723.html

标签:

0 评论

发表评论

您的电子邮件地址不会被公开。 必填的字段已做标记 *