一个例子能理解吧,
不要使用typeof、decltype来定义template<typename _Tp >中的类型作为新变量类型,不然会出错。
用typeof、decltype来定义新变量类型,须要明确的变量类型。
#ifndef _MSC_VER
#include <cxxabi.h>
template <typename T>
inline const char* _demangle()
{
static char name[200];
size_t len=200;
abi::__cxa_demangle( typeid( T ).name(),name,&len,0);
return name;
}
#define __TYPEID(v) _demangle<decltype(v)>()
//#define __TYPEID(v) abi::__cxa_demangle( typeid( v ).name(),0,0,0) // 会做成内存泄漏,会做释放
#else
#define __TYPEID(v) typeid( v ).name()
#endif
template<typename _Tp >
_Tp ko;
Json::Value json;
std::string k;
typeof (_Tp) rrh;// msvc 编译无法通过,mingw 通过
rrh=ko;
decltype (_Tp) rrth;// msvc 和 mingw 编译无法通过,
rrth=ko;
typeof (k) rr;// msvc 编译无法通过,mingw 通过
rr="123";
decltype (k) rrt;// msvc 和 mingw 通过
rrt="123";
std::cout<<__TYPEID(ko) << __TYPEID(json)<<__TYPEID(decltype(ko))<<__TYPEID(decltype(json));