Last active
July 5, 2016 16:03
-
-
Save PIlin/ec4befdcc505addf950ef289efe2c447 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
template <typename T> | |
void func(T& t) | |
{ | |
t.method(); | |
} | |
struct A { void method() { std::cout <<"a"; } }; | |
struct B { int justSomeValue; }; | |
void func(B& b) { std::cout << b.justSomeValue; } | |
void func(int& i) { std::cout << i; } | |
void func(float& f) { std::cout << f; } | |
struct SFail {}; | |
int main() | |
{ | |
A a; | |
func(a); // ok | |
B b; | |
func(b); // ok | |
int i = 42; | |
func(i); // ok | |
float f = 42.0f; | |
func(f); // ok | |
SFail sfail; | |
func(sfail); // fail | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename T> | |
bool func(T& t) | |
{ | |
return t.method(); | |
} | |
bool func(int& i) { return 42; } | |
struct SOk { bool method(); }; | |
struct SFail {}; | |
int main() | |
{ | |
int i = 42; | |
func(i); // ok | |
SOk sok; | |
func(sok); // ok | |
SFail sfail; | |
//func(sfail); // compile error | |
return 0; | |
} | |
template <typename T> | |
struct IsTypeBool | |
{ | |
enum { value = 0 }; | |
}; | |
template <> | |
struct IsTypeBool<bool> | |
{ | |
enum { value = 1}; | |
}; | |
void main2() | |
{ | |
SFail sfail; | |
static_assert(IsTypeBool<decltype(func(sfail))>::value, ""); // assert is not failed! | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment