在编译时打印模板类型名 [英] Print template typename at compile time

查看:507
本文介绍了在编译时打印模板类型名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中创建模板函数时,有一个简单的方法让模板的typename表示为字符串?我有一个简单的测试用例来显示我想要做的事情(注意显示的代码不编译):

When creating a template function in C++ is there a simple way to have the typename of the template represented as a string? I have a simple test case to show what I'm trying to do (note the code shown does not compile):

#include <stdio.h>
template <typename type>
type print(type *addr) 
{
  printf("type is: %s",typename);
}

int main()
{
  int a;
  print(&a);
}

// Would like to print something like:
// type is: int

我认为typename应该在编译时可用,当函数被实例化,但我不熟悉模板,我还没有看到一个方法来获取类型名称一个字符串。

I think that the typename should be available at compile time when the function is instantiated, but I'm not that familiar with templates and I haven't seen a way to get the typename as a string.

我想这样做的原因是为了一些printf类型调试。我有多个线程运行和步进通过gdb更改程序行为。所以对于某些事情,我想转储有关哪些函数正在执行的信息。这不是太重要,所以如果解决方案过于复杂,我会跳过将此信息添加到我的日志功能。

The reason that I want to do this is for some printf type debugging. I have multiple threads running and stepping through with gdb changes the program behavior. So for some things I want to dump information about which functions were executing. It's not too important so if the solution is overly complex I would skip adding this information to my logging function. But if there was a simple way to do this it would be useful information to have.

推荐答案

PRETTY_FUNCTION 应该解决你的问题(至少在运行时)

PRETTY_FUNCTION should solve your problem (at run time at least)

以下程序的输出是:

asfdasdfasdf test :: test() with type = int]

asfdasdfasdf test :: test()[with type = int]

asfdasdfasdf test :: test()[with type = int]

asfdasdfasdf test :: test()[with type = int]

asfdasdfasdf test :: test()[with type = int]

asfdasdfasdf test :: test() with type = int]

asfdasdfasdf void tempFunction()[with type = bool]

asfdasdfasdf void tempFunction()[with type = bool]

asfdasdfasdf void tempFunction()[with type = bool]

asfdasdfasdf void tempFunction()[with type = bool]

asfdasdfasdf void tempFunction()[with type = bool]

asfdasdfasdf void tempFunction()[with type = bool]

!!! Hello World!

The output to the program below is:
asfdasdfasdf test::test() [with type = int]
asfdasdfasdf test::test() [with type = int]
asfdasdfasdf test::test() [with type = int]
asfdasdfasdf test::test() [with type = int]
asfdasdfasdf test::test() [with type = int]
asfdasdfasdf test::test() [with type = int]
asfdasdfasdf void tempFunction() [with type = bool]
asfdasdfasdf void tempFunction() [with type = bool]
asfdasdfasdf void tempFunction() [with type = bool]
asfdasdfasdf void tempFunction() [with type = bool]
asfdasdfasdf void tempFunction() [with type = bool]
asfdasdfasdf void tempFunction() [with type = bool]
!!!Hello World!!!

如果你真的,真的,需要typename作为一个字符串,你可以hack这个(使用snprintf而不是printf),并拉取子字符串'='和beflore']'。

If you really, really, need the typename as a string, you could hack this (using snprintf instead of printf) and pull the substring after '=' and beflore ']'.

#include <iostream>
using namespace std;

template<typename type>
class test
{
public:
test()
{
    printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
    printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
    printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
    printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
    printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
    printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
}
};

template<typename type>
void tempFunction()
{
        printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
printf("asfdasdfasdf %s\n", __PRETTY_FUNCTION__);
}

int main() {
test<int> test;

tempFunction<bool>();
cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!!
return 0;

}

这篇关于在编译时打印模板类型名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆