Skip to content

Instantly share code, notes, and snippets.

@heatblazer
Last active September 7, 2019 16:59
Show Gist options
  • Save heatblazer/87ff6ce90892224dc3ad803a66532328 to your computer and use it in GitHub Desktop.
Save heatblazer/87ff6ce90892224dc3ad803a66532328 to your computer and use it in GitHub Desktop.
#include <iostream>
#if 0
#define BINDABLE \
template<typename Type1, typename Type2> \
friend void* bind(const Type1& t1, const Type2& t2); \
\
template<typename Type1, typename Type2> \
friend void* bind(Type1* t1, Type2* t2); \
\
template<typename Type1, typename Type2> \
friend void* bind(Type1 const &t1, Type2 const& t2); \
\
template<typename Type1, typename Type2> \
friend void* bind(Type1 t1, Type2 t2);
#endif
#define BINDABLE \
template<typename Type1, typename Type2> \
friend void* bind(Type1* t1, Type2* t2); \
\
template<typename Type1, typename Type2> \
friend void* bind(const Type1& t1, const Type2& t2);
class A
{
BINDABLE
int a;
public:
A(const A& ref) = default ; // test std::is_trivially_copy_constructible<>(.T.)
A() : a (20) {}
~A() {}
};
class B
{
BINDABLE
int b;
public:
B(const B& ref) = default ;
B() : b(10) {}
~B() {}
};
template<>
void* bind(A a, B b)
{
std::cout << "Called by value\r\n";
std::cout << "[" << a.a + b.b << "]\r\n";
}
template<>
void* bind(const A& a, const B& b)
{
std::cout << "Called by ref\r\n";
std::cout << "[" << a.a + b.b << "]\r\n";
}
template<>
void* bind(A* a, B* b)
{
std::cout << "Called by ptr\r\n";
std::cout << "[" << a->a + b->b << "]\r\n";
}
template <class T>
struct U
{
typename std::enable_if<std::is_union<T>{}, T>::type m_ref;
U(const T& ref) : m_ref{ref} { }
};
union tst
{
int val;
};
int main()
{
A a;
B b;
tst vvv = {10};
U<tst> rrr {vvv};
bind(a, b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment