In the code I attached I am looking for a way to map 1 type to another. The code fails to compile with the error
[root@]# g++ -std=c++11 test.cpp -o test; ./test
test.cpp: In member function ‘void A::Func(AType)’:
test.cpp:32:58: error: template argument 1 is invalid
using BType = std::result_of<GetBTypeFromAType(AType)>::type;
The compiler version I'm using is 5.3.1, any idea how to fix that?
#include <iostream>
#include <typeinfo>
#include <cxxabi.h>
#include <memory>
template <class T>
std::string GetTypeName()
{
//http://stackoverflow.com/questions/23266391/find-out-the-type-of-auto/23266701#23266701
std::unique_ptr<char, void(*)(void*)> name{abi::__cxa_demangle(typeid(T).name(), 0, 0, nullptr), std::free};
return name.get();
}
struct A
{
struct AA {};
struct AB {};
struct AC {};
struct BA {};
struct BB {};
struct BC {};
BA GetBTypeFromAType(AA a) { return BA(); }
BB GetBTypeFromAType(AB a) { return BB(); }
BC GetBTypeFromAType(AC a) { return BC(); }
template <typename AType>
void Func(AType a)
{
using BType = std::result_of<GetBTypeFromAType(AType)>::type;
std::cout << "Func called with " << GetTypeName<AType>() << " and got " << GetTypeName<BType>() << std::endl;
}
};
int main()
{
A a;
A::AA aa;
A::AB ab;
A::AC ac;
a.Func(aa);
a.Func(ab);
a.Func(ac);
return 0;
}