std :: to_string未在aix中声明编译错误

时间:2014-12-17 06:07:45

标签: c++ linux c++11 aix

我正在AIX env中编译我的代码..它给了我错误" std :: to_string"没有宣布 在Windows中成功编译了相同的代码。

define LOG_MSG(message) CLogManager::LogMessage(CLogManager::CurrentDateTime() + " - " + std::string(__FILE__) + "[" + std::to_string(static_cast<_ULonglong>(__LINE__)) + "] : " + std::string(message) + "\n")

这是宏,我将其用作

LOG_MSG(" ** BEGIN StorePasswordFromFile()");

此宏用于记录目的

1 个答案:

答案 0 :(得分:0)

我不确定最新xlC 14.1(或您正在使用的任何版本)对std::to_string()的支持程度。

如果支持不完整,C有一个隐藏的双宏方法(a)__LINE__转换为C字符串,这样你就可以使用std::string,与__FILE__message项目相同,看起来C ++预处理器一直忠实于其可怕的根源: - )

代码:

#include <stdio.h>
#define STR1(x) # x
#define STR2(x) STR1(x)
int main(void) {
    char x[] = __FILE__;
    char y[] = STR2(__LINE__);
    printf("file = %s, line = %s\n",x,y);
    return 0;
}

输出:

file = qq.c, line = 6

显示__LINE__已成功转换为C字符串值。

您应该可以在宏中使用类似的方法:

#define STR1(x) # x
#define STR2(x) STR1(x)
#define LOG_MSG(message) CLogManager::LogMessage( \
    CLogManager::CurrentDateTime() + " - " + \
    std::string(__FILE__) + "[" + \
    std::string(STR2(__LINE__)) + "] : " + \
    std::string(message) + "\n")

int main() {
    LOG_MSG ("My hovercraft is full of eels");
    return 0;
}

使用g++ -E qq.cpp进行的预处理可以为您提供:

  CLogManager::LogMessage( CLogManager::CurrentDateTime() + " - " + std::string("qq.cpp") + "[" + std::string("10") + "] : " + std::string("My hovercraft is full of eels") + "\n");

(仅显示相关行),似乎与您想要的相符。

然而,作为旁注,因为您似乎可以添加像"[" 这样的C字符串,而需要明确地构造字符串,我不确定您根本需要 std::string()来电。您仍然需要C宏hack来将整数转换为C字符串,但是,一旦完成,您应该能够按原样使用它。

将最终宏更改为:

#define LOG_MSG(message) CLogManager::LogMessage( \
    CLogManager::CurrentDateTime() + " - " + \
    __FILE__ + "[" + \
    STR2(__LINE__) + "] : " + \
    message + "\n")

会给你:

CLogManager::LogMessage( CLogManager::CurrentDateTime() + " - " + "qq.cpp" + "[" + "10" + "] : " + "My hovercraft is full of eels" + "\n");

这是否是一个好的想法,我会留给更广泛的社区,但它至少可以解决你的问题。我可能将整个批次放在#if/#else/#endif内,以便知道std::to_string()的C ++ 11编译器可以使用更接受的方法。


(a)如果您对为什么有兴趣,我将在下面解释。

根据###C11 6.10.3.4 /1宏运算符实际上优先于宏替换的递归性质:

  

替换列表中的所有参数都已替换为#和##   处理已经发生,所有地标预处理令牌都被删除。该   然后重新扫描所得到的预处理标记序列以及随后的所有标记序列   预处理源文件的标记,以替换更多的宏名称。

这意味着代码:

#define STR(x) # x
STR(__LINE__)

实际上会产生"__LINE__",因为#首先发生,并且一旦发生,字符串文字中的__LINE__不会被进一步替换。通过两步过程:

#define STR1(x) # x
#define STR2(x) STR1(x)
STR2(__LINE__)

第一级替换将STR2(__LINE__)变为STR1(3),因为__LINE__本身会受到扩展。

然后第二个级别STR1(3)通过# 3转为"3"

也许以下内容可能有所帮助:

#define STR1(x) # x
#define STR2a(x) STRn(x)
#define STR2b(x) STR1(x)

STR1(__LINE__)
STR2a(__LINE__)
STR2b(__LINE__)

注释的输出是:

"__LINE__"  - stringise, no further processing of __LINE__ inside literal.
STRn(6)     - shows first stage of replacement, line number replacement.
"7"         - shows full process.