#include <iostream>
#include <typeinfo>
using namespace std;
struct mystruct{};
template<typename T>
struct map;
//specification
#define MAPPING(Key, Val) \
template<> \
struct map<Key> \
{ \
typedef Val mapping_type; \
};
MAPPING(mystruct, int)
template<typename T>
void func(T t)
{
map<T>::mapping_type i = 999;
cout<<i<<endl;
}
int main() {
// your code goes here
mystruct ms;
func(ms);
return 0;
}
I try to do some type mapping(here mapping mystruct to int) via specification, but it can't compiled by gcc4.8.1, help!
or what is the right way to accomplish this, thanks!
Here are the error messages:
prog.cpp: In function ‘void func(T)’:
prog.cpp:23:2: error: need ‘typename’ before ‘map<T>::mapping_type’ because ‘map<T>’ is a dependent scope
map<T>::mapping_type i = 999;
^
prog.cpp:23:23: error: expected ‘;’ before ‘i’
map<T>::mapping_type i = 999;
^
prog.cpp:24:8: error: ‘i’ was not declared in this scope
cout<<i<<endl;
^
prog.cpp: In instantiation of ‘void func(T) [with T = mystruct]’:
prog.cpp:31:9: required from here
prog.cpp:23:2: error: dependent-name ‘map<T>::mapping_type’ is parsed as a non-type, but instantiation yields a type
map<T>::mapping_type i = 999;
^
prog.cpp:23:2: note: say ‘typename map<T>::mapping_type’ if a type is meant