mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
sfinae hello world
This commit is contained in:
18
README.adoc
18
README.adoc
@@ -20224,10 +20224,6 @@ Programs under link:userland/cpp/[] are examples of https://en.wikipedia.org/wik
|
|||||||
**** http://stackoverflow.com/questions/180172/default-constructor-with-empty-brackets
|
**** http://stackoverflow.com/questions/180172/default-constructor-with-empty-brackets
|
||||||
** `virtual` and polymorphism
|
** `virtual` and polymorphism
|
||||||
*** link:userland/cpp/virtual.cpp[]
|
*** link:userland/cpp/virtual.cpp[]
|
||||||
* templates
|
|
||||||
** link:userland/cpp/template.cpp[]: basic example
|
|
||||||
** link:userland/cpp/template_class_with_static_member.cpp[]: https://stackoverflow.com/questions/3229883/static-member-initialization-in-a-class-template
|
|
||||||
** link:userland/cpp/if_constexpr.cpp[]: C++17 `if constexpr`: https://stackoverflow.com/questions/12160765/if-else-at-compile-time-in-c/54647315#54647315
|
|
||||||
* iostream
|
* iostream
|
||||||
** link:userland/cpp/copyfmt.cpp[]: `std::copyfmt` restores stream state, see also: https://stackoverflow.com/questions/12560291/set-back-default-floating-point-print-precision-in-c/53673686#53673686
|
** link:userland/cpp/copyfmt.cpp[]: `std::copyfmt` restores stream state, see also: https://stackoverflow.com/questions/12560291/set-back-default-floating-point-print-precision-in-c/53673686#53673686
|
||||||
* fstream
|
* fstream
|
||||||
@@ -20511,6 +20507,18 @@ https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-sta
|
|||||||
|
|
||||||
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf
|
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/n4659.pdf
|
||||||
|
|
||||||
|
[[cpp-templates]]
|
||||||
|
==== C++ templates
|
||||||
|
|
||||||
|
* link:userland/cpp/template.cpp[]: basic example
|
||||||
|
* link:userland/cpp/template_class_with_static_member.cpp[]: https://stackoverflow.com/questions/3229883/static-member-initialization-in-a-class-template
|
||||||
|
|
||||||
|
===== SFINAE
|
||||||
|
|
||||||
|
https://en.cppreference.com/w/cpp/language/sfinae
|
||||||
|
|
||||||
|
Not possible to do the typecheck automatically without explicitly giving type constraints: https://stackoverflow.com/questions/53441832/sfinae-automatically-check-that-function-body-compiles-without-explicit-constrai
|
||||||
|
|
||||||
[[cpp-type-casting]]
|
[[cpp-type-casting]]
|
||||||
==== C++ type casting
|
==== C++ type casting
|
||||||
|
|
||||||
@@ -20521,6 +20529,8 @@ https://stackoverflow.com/questions/332030/when-should-static-cast-dynamic-cast-
|
|||||||
[[cpp-compile-time-magic]]
|
[[cpp-compile-time-magic]]
|
||||||
==== C++ compile time magic
|
==== C++ compile time magic
|
||||||
|
|
||||||
|
* link:userland/cpp/if_constexpr.cpp[]: C++17 `if constexpr`: https://stackoverflow.com/questions/12160765/if-else-at-compile-time-in-c/54647315#54647315
|
||||||
|
|
||||||
[[cpp-decltype]]
|
[[cpp-decltype]]
|
||||||
===== C++ `decltype`
|
===== C++ `decltype`
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
// https://cirosantilli.com/linux-kernel-module-cheat#cpp
|
// https://cirosantilli.com/linux-kernel-module-cheat#cpp-compile-time-magic
|
||||||
|
|
||||||
|
|
||||||
#if __cplusplus >= 201703L
|
#if __cplusplus >= 201703L
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|||||||
40
userland/cpp/sfinae.cpp
Normal file
40
userland/cpp/sfinae.cpp
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
// https://cirosantilli.com/linux-kernel-module-cheat#cpp-templates
|
||||||
|
|
||||||
|
#include <cassert>
|
||||||
|
|
||||||
|
struct HasMyfunc {
|
||||||
|
int myfunc() const { return 1; }
|
||||||
|
};
|
||||||
|
struct NoHasMyfunc {
|
||||||
|
int myfunc2() const { return 11; }
|
||||||
|
};
|
||||||
|
|
||||||
|
// int() is the actual return type.
|
||||||
|
// t.myfunc() checks that t has method myfunc().
|
||||||
|
// The decltype does not have multiple arguments, just a single comma separated expression:
|
||||||
|
// https://stackoverflow.com/questions/16044514/what-is-decltype-with-two-arguments
|
||||||
|
// Syntax breakdown:
|
||||||
|
// https://medium.com/@mortificador/choose-between-different-implementations-depending-on-type-properties-at-compile-time-in-c-68e3fd5cd2f8
|
||||||
|
template<class T>
|
||||||
|
auto template_func(const T& t) ->
|
||||||
|
decltype(t.myfunc(), int())
|
||||||
|
{
|
||||||
|
return t.myfunc();
|
||||||
|
}
|
||||||
|
|
||||||
|
template<class T>
|
||||||
|
auto template_func(const T& t) ->
|
||||||
|
decltype(t.myfunc2(), int())
|
||||||
|
{
|
||||||
|
return t.myfunc2();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// Hello world example.
|
||||||
|
// The correct template is used for the type that has each different method.
|
||||||
|
// If we didn't specify constraints for each template type, C++ would blow up
|
||||||
|
// with multiple possible functions:
|
||||||
|
// https://stackoverflow.com/questions/53441832/sfinae-for-function-body
|
||||||
|
assert(template_func(HasMyfunc()) == 1);
|
||||||
|
assert(template_func(NoHasMyfunc()) == 11);
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
// https://cirosantilli.com/linux-kernel-module-cheat#cpp
|
// https://cirosantilli.com/linux-kernel-module-cheat#cpp-templates
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
// https://cirosantilli.com/linux-kernel-module-cheat#cpp
|
// https://cirosantilli.com/linux-kernel-module-cheat#cpp-templates
|
||||||
|
|
||||||
#include <cassert>
|
#include <cassert>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user