可以在 constexpr 中获取 `__func__` 的值吗?

Possible to get value of `__func__` in a constexpr?

我有一个使用 __func__ 调用构造函数的宏。 是否可以将其转换为 constexpr 并仍然使用 "local"/"right" 版本的 func?

#define LOG_SCOPE_DURATION(category_arg) \
  ScopeDuration __scopeDuration = ScopeDurationConstructor(category_arg, __func__);

Possible to get value of __func__ in a constexpr?

是的。

Is it possible to convert [my macro] into a constexpr?

没有。如果在 constexpr 函数中使用 __func__,那么您将获得 constexpr 函数的名称;你不会得到任何调用函数的名称,当你展开预处理器宏时你会得到这个名称。

如果您使用的是 C++17 或更早版本,则无法将宏转换为函数。 您最接近的是创建一个接受位置并使用它的函数。

请注意,宏可以为您创建内联变量,函数永远不会向您当前的堆栈框架添加任何变量。因此,您可以将 [[nodiscard]] 添加到函数中,以不忽略您的 return 值。

template<typename T>
[[nodiscard]] constexpr ScopeDuration createScopeDuration(T &&t, const char *location)
{
    return ScopeDurationConstructor(std::forward<T>(t), location);
}
#define LOG_SCOPE_DURATION(category_arg) NS::createScopeDuration(category_arg, __func__)

用法:

 auto scope = LOG_SCOPE_DURATION(argument);

这基本上就是 ScopeDurationConstructor 函数。

希望从 C++20 开始,您可以使用 std::source_location,即 constexpr。 在这种情况下,您可以这样写:

template<typename T>
[[nodiscard]] constexpr ScopeDuration createScopeDuration(T &&t, const std::source_location& location = std::source_location::current()))
{
    return ScopeDurationConstructor(std::forward<T>(t), location.function_name());
}

用法:

auto scope = createScopeDuration(argument);

同样,您可以将其全部包含在 ScopeDuration 的构造函数中。

请注意,此功能已获准用于 C++20,但是,措辞尚未添加到标准中。有关详细信息,请参阅 trip report