The result of the following code I think should print “FUNC_TYPE::TYPE1 A&& “, but it doesn’t。And it reported an error。
“Error LNK2019: Unresolved external symbol ‘public: void* __cdecl A::dispatch<0, int, class A>(int, class A)’ (??dispatch@dispatch@0A@HVA@@@A@@QEAAPEAXHV0@@Z)”
#include <iostream>
enum class FUNC_TYPE {
TYPE1,
TYPE2
};
class A {
public:
template<FUNC_TYPE type, typename... T>
static void* __dispatch(A* a, T&& ...t) {
return a->dispatch<type,T...>(std::forward<T>(t)...);
}
template<FUNC_TYPE tag, typename... T>
void* dispatch(T... t);
};
template<>
void* A::dispatch<FUNC_TYPE::TYPE1>(int i, A&& a) {
std::cout << "FUNC_TYPE::TYPE1 A&& " << i << std::endl;
return nullptr;
}
template<>
void* A::dispatch<FUNC_TYPE::TYPE1>(int i, A& a) {
std::cout << "FUNC_TYPE::TYPE1 A& " << i << std::endl;
return nullptr;
}
A get() {
return A();
}
int main() {
A a;
A::__dispatch<FUNC_TYPE::TYPE1>(&a, 1, get());
}
What changes need to be made to ensure it works correctly?
And this is C++. Which concept does this pertain to? Are there any recommended materials for further reading?
3