Created
September 7, 2017 15:22
-
-
Save insooth/c6e4790f8424c36207c432c0c44d4795 to your computer and use it in GitHub Desktop.
Type of an expression
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <utility> | |
// based on: https://medium.com/@barryrevzin/value-categories-in-c-17-f56ae54bccbe | |
// LIVE: http://coliru.stacked-crooked.com/a/38321a888132b051 | |
template <class T> | |
struct E { static constexpr auto value = "prvalue\n"; }; | |
template <class T> | |
struct E<T&> { static constexpr auto value = "lvalue\n"; }; | |
template <class T> | |
struct E<T&&> { static constexpr auto value = "xvalue\n"; }; | |
struct A { int i; }; | |
int main () | |
{ | |
int&& r = 42; | |
// type of X expression is found by decltype( (X) ) | |
std::cout << E<decltype( (4) )>::value; // prvalue | |
std::cout << E<decltype( (r) )>::value; // lvalue | |
std::cout << E<decltype( (std::move(r)) )>::value; // xvalue | |
std::cout << E<decltype( (A{}) )>::value; // prvalue | |
std::cout << E<decltype( (A{}.i) )>::value; // xvalue (but printed prvalue) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment