c++ 函数的签名、参数的类型

  在函数模板参数类型推导,或者 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

 

转载于:https://my.oschina.net/mingyuejingque/blog/1057633

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值