Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
additional layer of indirection
//Microsoft (R) C/C++ Optimizing Compiler Version 19.00.23506 for x86 #include <type_traits> #include <iostream> int fn1() { return 1; } int fn2(int) { return 2; } template <typename FN_T, FN_T FN, typename DERIVED> class test_base { template <typename T = DERIVED, typename = std::enable_if_t<!T::fn_specified>> int fn() { return FN(); } public: int fn_indirect() { return static_cast<DERIVED*>(this)->fn(); } }; template <class FN_T, FN_T FN> struct FooT { using type = FN_T; static constexpr FN_T value = FN; }; template <class FN_T> class test_derived : public test_base<typename FN_T::type, FN_T::value, test_derived<FN_T>> { public: static constexpr bool fn_specified = false; }; template <typename FN_T> class test_derived<FooT<FN_T, &fn2>> : public test_base<FN_T, &fn2, test_derived<FooT<FN_T, &fn2>>> { using base = test_base<FN_T, &fn2, test_derived<FooT<FN_T, &fn2>>>; friend base; public: static constexpr bool fn_specified = true; int fn() { return fn2(1); } }; int main(void) { test_derived<FooT<decltype(&fn1), &fn1>> x1; test_derived<FooT<decltype(&fn2), &fn2>> x2; std::cout << x1.fn_indirect() << " "; // comment next line out and it will work std::cout << x2.fn_indirect() << std::endl; return 0; }
run
|
edit
|
history
|
help
0
Implements.cpp
😊
bad_cast
SFINAE with std::enable_if
test
namespace name resolution
MyCodeWindows
Error with move capture of a const ref in a lambda function
reference
Additional layer of indirection