c++ - std::to_string is not returned 在 aix 中声明编译错误

标签 c++ linux c++11 aix

我正在 AIX 环境中编译我的代码..它给我错误“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()");

该宏用于记录目的

最佳答案

我不确定最新的 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:

After all parameters in the replacement list have been substituted and # and ## processing has taken place, all placemarker preprocessing tokens are removed. The resulting preprocessing token sequence is then rescanned, along with all subsequent preprocessing tokens of the source file, for more macro names to replace.

这意味着代码:

#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.

关于c++ - std::to_string is not returned 在 aix 中声明编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27519315/

相关文章:

linux - 在 linux 中搜索和替换一行?

c++11 - 无法从模板类推导出方法的第二个模板参数

c++ - std::initializer_list 构造函数和 "braced initialization"问题

c++ - std::ostream 需要功能方面的帮助

c++ - 当数据隐藏在对象中时如何使用 intel prefetch pragma?

c++ - 覆盖在祖 parent 的构造函数中调用的祖 parent 的虚拟方法

c++ - omn​​et 中对 ISO c++ 2011 标准的库支持

c++ - 在 Arduino IDE 中链接预建对象

linux - UDP 套接字上的接收通知

c++ - 奇怪的线程堆栈