How to determine programmatically if an expression is rvalue or lvalue in C++?

前端 未结 4 766
甜味超标
甜味超标 2020-12-05 01:55

What\'s the best way to determine if an expression is a rvalue or lvalue in C++? Probably, this is not useful in practice but since I am learning rvalues and lvalues I thoug

4条回答
  •  天命终不由人
    2020-12-05 02:12

    Use std::is_lvalue_reference and std::is_rvalue_reference.

    You don't need a wrapper if you're happy with using decltype.

    std::string a("Hello");
    std::is_lvalue_reference::value; // false
    std::is_lvalue_reference::value; // true
    

    In C++17 you'll be able to use the following:

    std::string a("Hello");
    std::is_lvalue_reference_v; // false
    std::is_lvalue_reference_v; // true
    

    Or you could write a wrapper as @Ryan Haining suggests, just make sure you get the types correct.

提交回复
热议问题