Skip to content

Instantly share code, notes, and snippets.

@PIlin
Last active July 5, 2016 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PIlin/ec4befdcc505addf950ef289efe2c447 to your computer and use it in GitHub Desktop.
Save PIlin/ec4befdcc505addf950ef289efe2c447 to your computer and use it in GitHub Desktop.
#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
}
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