Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Member function detection
#include <iostream> using namespace std; struct A { void f() { cout << "A::f()\n"; } }; class B { }; void f() { cout << "::f()\n"; } // Compile-time function selection. template <class T, int S> struct Call { Call(T& t) {::f();} }; template <class T> struct Call<T, 1> { Call(T& t) { t.f(); } }; // Compile-time member detection. class C1 { char c[1]; }; class C2 { char c[2]; }; template<class T> C1 Detect(decltype(&T::f)); template<class T> C2 Detect(...); template <class T> void callF( T& t ) { Call<T, sizeof(Detect<T>(0))> call(t); } int main() { static_assert(sizeof(C1) == 1, ""); static_assert(sizeof(C2) == 2, ""); A a; B b; callF(a); callF(b); return 0; }
run
|
edit
|
history
|
help
0
ShoppingList
Sangharsh vhawale
NamespaceOverload
find first non repeating
DailyGroceryHisto
volatile thread-safe object
Dar
segmentedSieveD
Policy based smart pointer
reverse array