当宏用作变量名时,有什么方法可以跳过宏替换(在预处理期间)?

Is there any way to skip macro replacement(During preprocessing) when macro is used as a variable name?

ConflictHeader.h

      #define _c 6 //This is third party header, canot change, since
// there is no sorce code to rebuild

testclass.h

    #ifndef TESTCLASS_H
    #define TESTCLASS_H

    #include <QObject>
    #include "ConflictHeader.h"//Include conflicted header

    class TestClass : public QObject
    {
      Q_OBJECT

    public:
        TestClass(QObject *parent);
        ~TestClass();
    private:

    };
    #endif // TESTCLASS_H

testclass.cpp

#include "testclass.h"

TestClass::TestClass(QObject *parent)
  : QObject(parent)
{

}    
TestClass::~TestClass()
{

}

moc_testclass.cpp

This is generated by MOC compiler, Please note function where compilation issue comes "int TestClass::qt_metacall(QMetaObject::Call _c, int _id, void **_a)"

/****************************************************************************
** Meta object code from reading C++ file 'testclass.h'
**
** Created: Wed Jun 10 11:24:06 2015
**      by: The Qt Meta Object Compiler version 62 (Qt 4.7.4)
**
** WARNING! All changes made in this file will be lost!
*****************************************************************************/

#include "../../testclass.h"
#if !defined(Q_MOC_OUTPUT_REVISION)
#error "The header file 'testclass.h' doesn't include <QObject>."
#elif Q_MOC_OUTPUT_REVISION != 62
#error "This file was generated using the moc from 4.7.4. It"
#error "cannot be used with the include files from this version of Qt."
#error "(The moc has changed too much.)"
#endif

QT_BEGIN_MOC_NAMESPACE
static const uint qt_meta_data_TestClass[] = {

 // content:
       5,       // revision
       0,       // classname
       0,    0, // classinfo
       0,    0, // methods
       0,    0, // properties
       0,    0, // enums/sets
       0,    0, // constructors
       0,       // flags
       0,       // signalCount

       0        // eod
};

static const char qt_meta_stringdata_TestClass[] = {
    "TestClass[=13=]"
};

const QMetaObject TestClass::staticMetaObject = {
    { &QObject::staticMetaObject, qt_meta_stringdata_TestClass,
      qt_meta_data_TestClass, 0 }
};

#ifdef Q_NO_DATA_RELOCATION
const QMetaObject &TestClass::getStaticMetaObject() { return staticMetaObject; }
#endif //Q_NO_DATA_RELOCATION

const QMetaObject *TestClass::metaObject() const
{
    return QObject::d_ptr->metaObject ? QObject::d_ptr->metaObject : &staticMetaObject;
}

void *TestClass::qt_metacast(const char *_clname)
{
    if (!_clname) return 0;
    if (!strcmp(_clname, qt_meta_stringdata_TestClass))
        return static_cast<void*>(const_cast< TestClass*>(this));
    return QObject::qt_metacast(_clname);
}

int TestClass::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
{
    _id = QObject::qt_metacall(_c, _id, _a);
    if (_id < 0)
        return _id;
    return _id;
}
QT_END_MOC_NAMESPACE

Compiler output error 1>.\GeneratedFiles\Debug\moc_testclass.cpp(62) : error C2143: syntax error : missing ')' before 'constant'

所以我无法在 "ConflictHeader.h"(第三方库)和 "moc_testclass.cpp"(生成的 moc)中更改“_c”。

当宏被用作变量名时,有没有办法跳过宏替换(在预处理期间)?

使用 #undef 预处理器指令取消定义宏。

更多信息:https://gcc.gnu.org/onlinedocs/cpp/Undefining-and-Redefining-Macros.html

您应该可以通过以下方式解决该问题:

#include "ConflictHeader.h"//Include conflicted header
#ifdef _c
#undef _c
#endif

更好的选择可能是从 ConflictHeader.h 创建一个包装器 .h 文件并在其余代码中使用该包装器 .h 文件。

ConflictHeaderW.h:

#pragma once

#include "ConflictHeader.h"
#ifdef _c
#undef _c
#endif

// #undef anything else that create problems.