You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Resharper C++报Plog的LOGD宏‘this’标识符错误,求修改方案

Fixing Resharper C++'s "Expected Identifier" Warning for Plog's LOGD Macro

I’ve run into this exact frustration with Resharper C++ flagging this in Plog macros that use Microsoft’s __if_exists extension. Let’s break down why this happens and how to fix it cleanly.

Why Resharper Flags This

Resharper’s code analysis engine doesn’t fully handle non-standard constructs like __if_exists/__if_not_exists when they’re embedded directly in macro expansions. It gets confused by the conditional scope and incorrectly flags this as an invalid identifier, even though the code compiles fine with MSVC.

Solution 1: Replace __if_exists with a Compiler Intrinsic

The cleanest fix is to swap the __if_exists check for a compiler intrinsic that Resharper recognizes. MSVC (and most compilers used with Resharper) supports __is_member_function(), which returns 1 if the current context is a member function, 0 otherwise. This lets us use a simple ternary to safely pass this or nullptr:

#define LOGD(message) \
    do { \
        auto logger = plog::get<0>(); \
        if (logger && logger->checkSeverity(plog::debug)) { \
            const void* obj = __is_member_function() ? static_cast<const void*>(this) : nullptr; \
            (*logger) += plog::Record(plog::debug, __FUNCTION__, __LINE__, __FILE__, obj) << message; \
        } \
    } while(false)

Note: I replaced your hardcoded line number 151 with __LINE__—this ensures the log shows the actual line where LOGD is called, which is way more useful for debugging.

Solution 2: Suppress the Warning Directly

If you don’t want to rewrite the macro, you can tell Resharper to ignore this specific warning for the macro. Add a Resharper-specific comment above the macro definition:

// ReSharper disable once CppInvalidIdentifier
#define LOGD(message) \
    if (!plog::get<0>() || !plog::get<0>()->checkSeverity(plog::debug)) {;} else (*plog::get<0>()) += plog::Record(plog::debug, __FUNCTION__, __LINE__, "", __if_exists(this) { this } __if_not_exists(this) { 0 }) << message;

Or use Resharper’s suppress attribute for a more formal approach:

[[jetbrains::suppress("CppInvalidIdentifier")]]
#define LOGD(message) \
    if (!plog::get<0>() || !plog::get<0>()->checkSeverity(plog::debug)) {;} else (*plog::get<0>()) += plog::Record(plog::debug, __FUNCTION__, __LINE__, "", __if_exists(this) { this } __if_not_exists(this) { 0 }) << message;

This is a quick workaround, but it doesn’t fix the underlying parsing issue—just hides the warning.

Solution 3: Cross-Compiler Safe Helper Function

If you need your code to work with compilers beyond MSVC, use a helper function with overloading to detect the member function context:

#include <type_traits>

namespace plog_detail {
    // Overload for member functions (accepts `this`)
    template<typename T>
    const void* get_object_ptr(const T* obj, std::true_type) {
        return obj;
    }

    // Overload for non-member functions (returns nullptr)
    const void* get_object_ptr(..., std::false_type) {
        return nullptr;
    }
}

// Helper macro to safely get `this` or nullptr
#define PLOG_GET_OBJECT() \
    plog_detail::get_object_ptr(this, std::is_class<std::decay_t<decltype(*this)>>{})

// Updated LOGD macro
#define LOGD(message) \
    do { \
        auto logger = plog::get<0>(); \
        if (logger && logger->checkSeverity(plog::debug)) { \
            (*logger) += plog::Record(plog::debug, __FUNCTION__, __LINE__, __FILE__, PLOG_GET_OBJECT()) << message; \
        } \
    } while(false)

This uses C++ type traits to determine if this is valid, and Resharper’s analysis engine handles this cleanly without warnings.

My Top Pick

Go with Solution 1 if you’re working exclusively with MSVC (the most common setup for Resharper C++). It’s concise, maintains the original macro’s intent, and eliminates the warning without extra boilerplate.

内容的提问来源于stack exchange,提问作者Jeff

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:25:14