字符串化模板参数

发布于 2024-09-06 02:43:00 字数 315 浏览 1 评论 0原文

是否可以获得模板参数名称的字符串化版本?

像这样,如果我们运行预处理器的话:

template <typename T>
struct Named{
    const char* name(){ return "Named<" #T ">"; }
};

编辑 复制。看这里 字符串化模板参数

Is it possible to get a stringified version of a template argument name?

Something like this, if only we were running the preprocessor:

template <typename T>
struct Named{
    const char* name(){ return "Named<" #T ">"; }
};

Edit
Duplicate. See here
Stringifying template arguments

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

流年里的时光 2024-09-13 02:43:00

不。您可以拥有的最接近的东西是 typeid(T).name()。然而,其结果是未指定的,即使是为所有类型返回空字符串的实现也是一致的。不过,出于调试目的,它通常就足够了。

No. The closest thing you can have is typeid(T).name(). However, the result of this is unspecified, even an implementation which returned empty strings for all types would be conforming. For debugging purposes it often is sufficient, though.

箜明 2024-09-13 02:43:00

您是否按照字符串化模板参数中的建议尝试过typeid()

Have you tried typeid() as suggested in Stringifying template arguments ?

金橙橙 2024-09-13 02:43:00

并非没有痛苦。我最接近的解决方案:

template <typename T>
struct Named{
    const char* name();
};

#define DEFINE_NAMED(T) template<> const char* Named<T>::name(){ return #T ; };

DEFINE_NAMED(SomeNameSpace::SomeClass)

当然,这是邪恶的......
到目前为止,您可以使用 gccxmlxsltproc 自动找到未实现的 Named::name(),创建一些辅助文件,编译它,最后链接它:

gccxml test.cpp -fxml=test.xml
xsltproc -o Named.cpp Named.xslt test.xml
g++ Named.cpp test.cpp -o test.bin

一些建议 Named.xslt 文件(不知道是否有效):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="text" indent="yes" encoding="utf-8" />

<xsl:template match="Method" >
    <xsl:text>template<> const char* </xsl:text>
    <xsl:value-of select="@demangled" />
    <xsl:text> { return "</xsl:text>
    <xsl:value-of select="substring(@demangled,7,string-length(@demangled)-15)" />
    <xsl:text>"; };
</xsl:text>
</xsl:template>

<xsl:template match="/">
    <xsl:text>#include "Named.h"
</xsl:text>
    <xsl:apply-templates select="/GCC_XML/Method[matches(@demangled,'^Named.*::name()
) and @extern = '1' ]" />
</xsl:template>

</xsl:stylesheet>

Not without pain. My closest solution:

template <typename T>
struct Named{
    const char* name();
};

#define DEFINE_NAMED(T) template<> const char* Named<T>::name(){ return #T ; };

DEFINE_NAMED(SomeNameSpace::SomeClass)

Of course, this is evil...
so far you could use gccxml and xsltproc to automatic find unimplemented Named<T>::name(), create some auxilary file, compile it, and finally link it:

gccxml test.cpp -fxml=test.xml
xsltproc -o Named.cpp Named.xslt test.xml
g++ Named.cpp test.cpp -o test.bin

Some proposal Named.xslt file (duno if work):

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="text" indent="yes" encoding="utf-8" />

<xsl:template match="Method" >
    <xsl:text>template<> const char* </xsl:text>
    <xsl:value-of select="@demangled" />
    <xsl:text> { return "</xsl:text>
    <xsl:value-of select="substring(@demangled,7,string-length(@demangled)-15)" />
    <xsl:text>"; };
</xsl:text>
</xsl:template>

<xsl:template match="/">
    <xsl:text>#include "Named.h"
</xsl:text>
    <xsl:apply-templates select="/GCC_XML/Method[matches(@demangled,'^Named.*::name()
) and @extern = '1' ]" />
</xsl:template>

</xsl:stylesheet>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文