在函数模板参数类型推导,或者 auto, typedef 表达式中的类型推导有帮助的功能代码:
在g++里有__FUNCTION__, __func__, __PRETTY_FUNCTION__
在vs2017 里智能提示中有__PRETTY_FUNCTION__ , __FUNCSIG__
boost 里有
#include <boost/current_function.hpp>
#include <boost/type_index.hpp>
template<class T>
void func(T t) {
boost::detail::current_function_helper();
std::cout << "BOOST_CURRENT_FUNCTION : " << BOOST_CURRENT_FUNCTION << std::endl;
std::cout << "T: "
<< boost::typeindex::type_id_with_cvr<T>().pretty_name()
<< std::endl;
std::cout << "t: "
<< boost::typeindex::type_id_with_cvr<decltype(t)>().pretty_name()
<< std::endl;
}
测试代码:
int main()
{
printf("hello world!\n");
int x = 1;
test(x);
char *p = nullptr;
test(p);
char sz[8];
test(p);
const int &r = x;
test(r);
const int &&rr = std::move(x);
test(rr);
return 0;
}
输出:
hello world!
BOOST_CURRENT_FUNCTION : void __cdecl test<int>(const int &)
T: int
t: int const & __ptr64
not a pointer
BOOST_CURRENT_FUNCTION : void __cdecl test<char*>(char *const &)
T: char * __ptr64
t: char * __ptr64 const & __ptr64
pointer
BOOST_CURRENT_FUNCTION : void __cdecl test<char*>(char *const &)
T: char * __ptr64
t: char * __ptr64 const & __ptr64
pointer
BOOST_CURRENT_FUNCTION : void __cdecl test<int>(const int &)
T: int
t: int const & __ptr64
not a pointer
BOOST_CURRENT_FUNCTION : void __cdecl test<int>(const int &)
T: int
t: int const & __ptr64
not a pointer