Skip to content

Instantly share code, notes, and snippets.

@mosolovsa
Created March 10, 2021 04:54
Show Gist options
  • Save mosolovsa/78ed8434a8ee2ef16765f112a17f3f08 to your computer and use it in GitHub Desktop.
Save mosolovsa/78ed8434a8ee2ef16765f112a17f3f08 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <memory>
#include <cxxabi.h> // <-- gcc header
template <typename type> std::unique_ptr<const char*> type_name(const type *addr)
{ return std::make_unique<const char *>(abi::__cxa_demangle(typeid(*addr).name(), 0, 0, NULL)); }
template<typename T> struct MyTest {
MyTest(T _data) : data(_data) { }
template<typename U> MyTest<T> operator+(MyTest<U> oth) {
return this->data + oth.data;
}
friend std::ostream &operator<<(std::ostream &os, const MyTest<T> &test) {
os << "MyTest [data: " << test.data << " of type <" << *type_name(&test.data) << ">]";
return os;
}
template<typename U> friend class MyTest;
private:
T data;
};
int main(int argc, char *argv[]) {
MyTest a(1);
MyTest b(1.0);
auto c = a + b;
std::cerr << "c: " << c << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment