使用std :: tie来比较结构

时间:2016-10-12 09:43:53

标签: c++ qt overloading operator-keyword tie

我在Qt C ++的应用程序中有这个代码。用于比较结构的operator==总是返回false,即使它们相等。我的代码出了什么问题?

以下是一个存在问题的代码段:

struct pSettings
{
    int speciality;
    bool autoCompleteByWord;
    bool showChronicConditions;
    bool showNavArrows;
    bool smallScreenMode;
    bool simpleExamination;
    bool alwaysSave;
    bool inLinePatientList;
    double newVisitPrice;
    double followVisitprice1;
    double followVisitprice2;
    double followVisitprice3;
    double followVisitprice4;
    int autosaveinterval;
    bool  autoSave ;
    bool minimizeToTray;
    int selectedTheme;
    QString textColor;
    QString topBGColor;
    QString bottomBGColor;
    QString altWinColor;
    QString buttonColor;
    QString altButtonColor;
    QString textBGColor;
    QString borderColor1;
    QString borderColor2;
    QString altButtonColorHover;
    QString buttonColorHover;
    QString buttonColorDisabled;
    QString buttonBorderColorHover;
    QString comboBGcolor;
    QString buttonTextColor;
    QString comboTextColor;
    double lOffSet,tOffSet;

    QString defaultFont;
    double defaultFontSize;
    bool defaultFontBold;

    QString textboxFont;
    double textboxFontSize;
    bool textboxFontBold;

    int maxFollowUps;
    bool autoClosePrintDlg;
    int maxFollowUpsPerProblem;
    bool autoSetnewAfterMaxPerProblemIsReached;
    int checkoutDate;
    int checkoutTime;
    bool enableVisualEffects;

    bool operator==(const pSettings& psettings) const
    {
        return std::tie(
                    speciality,
                    autoCompleteByWord,
                    showChronicConditions,
                    showNavArrows,
                    smallScreenMode,
                    simpleExamination,
                    alwaysSave,
                    inLinePatientList,
                    newVisitPrice,
                    followVisitprice1,
                    followVisitprice2,
                    followVisitprice3,
                    followVisitprice4,
                    autosaveinterval,
                    autoSave ,
                    minimizeToTray,
                    selectedTheme,
                    textColor,
                    topBGColor,
                    bottomBGColor,
                    altWinColor,
                    buttonColor,
                    altButtonColor,
                    textBGColor,
                    borderColor1,
                    borderColor2,
                    altButtonColorHover,
                    buttonColorHover,
                    buttonColorDisabled,
                    buttonBorderColorHover,
                    comboBGcolor,
                    buttonTextColor,
                    comboTextColor,
                    lOffSet,
                    tOffSet,
                    defaultFont,
                    defaultFontSize,
                    defaultFontBold,
                    textboxFont,
                    textboxFontSize,
                    textboxFontBold,
                    maxFollowUps,
                    autoClosePrintDlg,
                    maxFollowUpsPerProblem,
                    autoSetnewAfterMaxPerProblemIsReached,
                    checkoutDate,
                    checkoutTime,
                    enableVisualEffects) ==
                std::tie(psettings.speciality,
                         psettings.autoCompleteByWord,
                         psettings.showChronicConditions,
                         psettings.showNavArrows,
                         psettings.smallScreenMode,
                         psettings.simpleExamination,
                         psettings.alwaysSave,
                         psettings.inLinePatientList,
                         psettings.newVisitPrice,
                         psettings.followVisitprice1,
                         psettings.followVisitprice2,
                         psettings.followVisitprice3,
                         psettings.followVisitprice4,
                         psettings.autosaveinterval,
                         psettings.autoSave ,
                         psettings.minimizeToTray,
                         psettings.selectedTheme,
                         psettings.textColor,
                         psettings.topBGColor,
                         psettings.bottomBGColor,
                         psettings.altWinColor,
                         psettings.buttonColor,
                         psettings.altButtonColor,
                         psettings.textBGColor,
                         psettings.borderColor1,
                         psettings.borderColor2,
                         psettings.altButtonColorHover,
                         psettings.buttonColorHover,
                         psettings.buttonColorDisabled,
                         psettings.buttonBorderColorHover,
                         psettings.comboBGcolor,
                         psettings.buttonTextColor,
                         psettings.comboTextColor,
                         psettings.lOffSet,
                         psettings.tOffSet,
                         psettings.defaultFont,
                         psettings.defaultFontSize,
                         psettings.defaultFontBold,
                         psettings.textboxFont,
                         psettings.textboxFontSize,
                         psettings.textboxFontBold,
                         psettings.maxFollowUps,
                         psettings.autoClosePrintDlg,
                         psettings.maxFollowUpsPerProblem,
                         psettings.autoSetnewAfterMaxPerProblemIsReached,
                         psettings.checkoutDate,
                         psettings.checkoutTime,
                         psettings.enableVisualEffects);
    }

};

2 个答案:

答案 0 :(得分:5)

在teo long列表中的某个位置,如果它们不相同,则会出现错误。你违反了干(不要重复自己)。

在C ++ 14中:

auto mytie() const {
  return std::tie( /* ... fields */ );
}

然后使用它编写==

在C ++ 11中,您必须将mytie(blah const& self)写为本地(无状态)lambda以保持合理。

如果失败了,那么他们会比较不平等,因为它们会以你没注意到的方式不同。

答案 1 :(得分:0)

您可以为此使用高阶宏功能:

#define ITEMS                   \
ITEM(int,  speciality)          \
ITEM(bool, autoCompleteByWord)  \
...
ITEM(int,  checkoutTime)        \
LAST(bool, enableVisualEffects)

#define ITEM(x,y) x y;
#define LAST(x,y) ITEM(x,y)

struct pSettings
{
    ITEMS
}

#define ITEM(x,y) y,
#define LAST(x,y) y

auto mytie() const
{
    return std::tie
    (
        ITEMS
    );
}

在单独的文件中写入ITEMS定义是一个好主意