C ++ Concatinating __func__和const char文字 [英] C++ Concatinating __func__ and const char literal

查看:149
本文介绍了C ++ Concatinating __func__和const char文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C ++中concordate __ func__和文字字符串,并且我在GCC(有或没有C ++ 11 standart)上出错。例如,像这样:

  #include  
int main(void)
{
printf(%s,__func__try#1 \\\
);
printf(__ func__try#2 \\\
);
返回0;
}

看起来像__ func__实际上不是字符串文字宏。有人知道为什么会发生这种情况,也许是解决这个问题的方法吗?

解决方案

从技术上讲, __ func__ 是一个预定义的标识符。它是从不实现为一个宏。

它的类型为 static const char __func __ [] std :: string 中。



使用



printf(%s try#1 \\\
,__func __);



代替。


I am trying to concatinate __ func__ and literal string in C++ and I am getting an error on GCC (with or without of C++11 standart). Like, for example, here:

#include <cstdio>

int main(void)
{
        printf("%s", __func__ " try #1\n");
        printf(__func__ " try #2\n");
        return 0;
}

Looks like __ func__ is not actually a string literal macros. Does anyone knows why that is happening and, maybe, way to solve it?

解决方案

Technically, __func__ is a predefined identifier. It's never implemented as a macro.

It has the type static const char __func__[] and so therefore you can't concatenate it the way you have. Of course, you can add it to a std::string though.

Use

printf("%s try #1\n", __func__);

instead.

这篇关于C ++ Concatinating __func__和const char文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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