gpt4 book ai didi

c-preprocessor - 跨编译器工作的函数名宏

转载 作者:行者123 更新时间:2023-12-04 04:33:01 28 4
gpt4 key购买 nike

有预处理器宏定义其中的函数名称,__func__对于 g++ 和 __FUNCTION__对于 CL。 ( func 是执行此操作的 C++ 标准方法(来自 dcl.fct.def.general),但并未在任何地方实现)

对于将由这两个编译器编译的源代码,我该怎么办?

我试过了

#ifdef __func__
#define FUNCTION_NAME __func__
#else
#ifdef __FUNCTION__
#define FUNCTION_NAME __FUNCTION__
#else
#error "Function name macro not found"
#endif
#endif

但由于它不在函数中,因此未定义宏。

我也试过
void implementation_detail() {
#ifdef __func__
#define FUNCTION_NAME __func__
#else
#ifdef __FUNCTION__
#define FUNCTION_NAME __FUNCTION__
#else
#error "Function name macro not found"
#endif
#endif
}

但这也会出错。

我该怎么做才能让它在多个编译器中工作?

最佳答案

我发现有一个 boost 宏,BOOST_CURRENT_FUNCTION。

从看源码here这是如何实现的,他们使用这样的内联函数。

inline void current_function_helper()
{

#if defined(__GNUC__) || (defined(__MWERKS__) && (__MWERKS__ >= 0x3000)) || (defined(__ICC) && (__ICC >= 600)) || defined(__ghs__) || defined(__DMC__)`
...

(它也在名为 detail 的命名空间中声明,这是非常明智的)

我会用这个。

编辑:实现 __func__ 现在将在 Visual Studio 客户技术预览中,参见 here .

关于c-preprocessor - 跨编译器工作的函数名宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20244726/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com