Visual Studio 2015 年的宏参数限制

Macro parameter restrictions in Visual Studio 2015

我正在使用 visual studio 2015 将一些代码移植到 windows,这些代码是使用 gcc 在 linux 中构建的。

在 VS 中,当使用带参数“+=”的宏 DEFOP 时出现错误 error C2059: syntax error: '+=',与其他参数相同。这是代码:

#define DEFOP(OP) \
    Matrix& Matrix::operator OP (const double& val) { \
        for (int i = 0; i < _n; i++) { \
            _data[i] OP val; \
        } \
    return *this; \
    } \
    Matrix& Matrix::operator OP (const Matrix& that) { \
    if (_rows != _rows || _cols != that._cols) { \
            throw Exception (String ( \
                "Matrix size mismatch in operation '%s': " \
        "(%d,%d) vs. (%d,%d).", \
                __STRING(OP), _rows, _cols, that._rows, that._cols)); \
    } \
        for (int i = 0; i < _n; i++) { \
            _data[i] OP that._data[i]; \
        } \
    return *this; \
    }
DEFOP(+=);
DEFOP(-=);
DEFOP(*=);
DEFOP(/=);
#undef DEFOP

有人知道如何修改此代码以便它在 VS 中构建吗?

由于__STRING是一个非标准的宏,并不是所有的编译器都接受它。将宏参数用作字符串文字的常用方法是使用 #,即您应该将 __STRING(OP) 替换为 #OP