mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
decltype
This commit is contained in:
14
README.adoc
14
README.adoc
@@ -20518,6 +20518,20 @@ link:userland/cpp/static_dynamic_reinterpret_cast.cpp[]
|
|||||||
|
|
||||||
https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used/60414256#60414256
|
https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-const-cast-and-reinterpret-cast-be-used/60414256#60414256
|
||||||
|
|
||||||
|
[[cpp-compile-time-magic]]
|
||||||
|
==== C++ compile time magic
|
||||||
|
|
||||||
|
[[cpp-decltype]]
|
||||||
|
===== C++ `decltype`
|
||||||
|
|
||||||
|
link:userland/cpp/decltype.cpp[]
|
||||||
|
|
||||||
|
C++11 keyword.
|
||||||
|
|
||||||
|
Replaces decltype with type of an expression at compile time.
|
||||||
|
|
||||||
|
More powerful than `auto` as you can use it in more places.
|
||||||
|
|
||||||
=== POSIX
|
=== POSIX
|
||||||
|
|
||||||
Programs under link:userland/posix/[] are examples of POSIX C programming.
|
Programs under link:userland/posix/[] are examples of POSIX C programming.
|
||||||
|
|||||||
69
userland/cpp/decltype.cpp
Normal file
69
userland/cpp/decltype.cpp
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
// https://cirosantilli.com/linux-kernel-module-cheat#cpp-decltype
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
#include <vector>
|
||||||
|
#include <utility> // declval
|
||||||
|
|
||||||
|
int f() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
class C {
|
||||||
|
public:
|
||||||
|
int f() { return 2; }
|
||||||
|
};
|
||||||
|
|
||||||
|
int i;
|
||||||
|
decltype(i) g() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
#if __cplusplus >= 201103L
|
||||||
|
// 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(typeid(i) == typeid(int));
|
||||||
|
|
||||||
|
C c;
|
||||||
|
decltype(c.f()) j;
|
||||||
|
assert(typeid(j) == typeid(int));
|
||||||
|
|
||||||
|
// Return value without instance. Use declval.
|
||||||
|
// http://stackoverflow.com/questions/9760358/decltype-requires-instantiated-object
|
||||||
|
decltype(std::declval<C>().f()) k;
|
||||||
|
assert(typeid(k) == typeid(int));
|
||||||
|
}
|
||||||
|
|
||||||
|
// decltype must take expressions as input, not a type.
|
||||||
|
// For types with the default constructor like `int`, we can just call the default constructor as in int():
|
||||||
|
// https://stackoverflow.com/questions/39279074/what-does-the-void-in-decltypevoid-mean-exactly
|
||||||
|
// or we can just use declval.
|
||||||
|
{
|
||||||
|
decltype(int()) i = 0;
|
||||||
|
assert(typeid(i) == typeid(int));
|
||||||
|
|
||||||
|
decltype(std::declval<int>()) j = 0;
|
||||||
|
assert(typeid(j) == typeid(int));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Can be used to declare the return value of functions.
|
||||||
|
assert(g() == 1);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user