mirror of
https://github.com/libretro/cpp-cheat.git
synced 2025-04-15 17:49:59 +00:00
56 lines
861 B
C++
56 lines
861 B
C++
/*
|
|
# decltype
|
|
|
|
C++11 keyword
|
|
|
|
Replace decltype with type of an expression at compile time.
|
|
|
|
More powerful than `auto`.
|
|
*/
|
|
|
|
#include "common.hpp"
|
|
|
|
int f() {
|
|
return 1;
|
|
}
|
|
|
|
class C {
|
|
public:
|
|
int f() { return 2; }
|
|
};
|
|
|
|
int main() {
|
|
#if __cplusplus >= 201103L
|
|
int i = 1;
|
|
float f = 2.0;
|
|
decltype(i + f) f2 = 1.5;
|
|
assert(f2 == 1.5);
|
|
|
|
// Implies reference while auto does not.
|
|
{
|
|
int i = 0;
|
|
int& ir = i;
|
|
decltype(ir) ir2 = ir;
|
|
ir2 = 1;
|
|
assert(i == 1);
|
|
}
|
|
|
|
// Can be used basically anywhere.
|
|
{
|
|
int i = 0;
|
|
std::vector<decltype(i)> v;
|
|
v.push_back(0);
|
|
}
|
|
|
|
// Return value.
|
|
{
|
|
decltype(f) i;
|
|
assert(sizeof(i) == sizeof(int));
|
|
|
|
C c;
|
|
decltype(c.f()) j;
|
|
assert(sizeof(i) == sizeof(int));
|
|
}
|
|
#endif
|
|
}
|