Skip to content

Instantly share code, notes, and snippets.

@insooth
Created September 7, 2017 15:22
Show Gist options
  • Save insooth/c6e4790f8424c36207c432c0c44d4795 to your computer and use it in GitHub Desktop.
Save insooth/c6e4790f8424c36207c432c0c44d4795 to your computer and use it in GitHub Desktop.
Type of an expression
#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