From a8aa4fdb7b43b7ba06b168a919c173781aaab645 Mon Sep 17 00:00:00 2001 From: Roman Telezhynskyi Date: Sat, 29 Jan 2022 11:50:13 +0200 Subject: [PATCH] Fix warning 'unused-lambda-capture'. --- src/app/puzzle/vpmainwindow.cpp | 7 +- .../dialogs/vwidgetbackgroundimages.cpp | 5 +- src/libs/vmisc/lambdaconstants.h | 68 +++++++++++++++++++ src/libs/vmisc/vmisc.pri | 1 + 4 files changed, 76 insertions(+), 5 deletions(-) create mode 100644 src/libs/vmisc/lambdaconstants.h diff --git a/src/app/puzzle/vpmainwindow.cpp b/src/app/puzzle/vpmainwindow.cpp index 833b0b90b..32fb3f481 100644 --- a/src/app/puzzle/vpmainwindow.cpp +++ b/src/app/puzzle/vpmainwindow.cpp @@ -61,6 +61,7 @@ #include "dialogs/dialogsavemanuallayout.h" #include "../vdxf/libdxfrw/drw_base.h" #include "../vmisc/dialogs/dialogselectlanguage.h" +#include "../vmisc/lambdaconstants.h" #if QT_VERSION < QT_VERSION_CHECK(5, 12, 0) #include "../vmisc/backport/qscopeguard.h" @@ -775,8 +776,8 @@ void VPMainWindow::InitPropertyTabCurrentPiece() ui->comboBoxTranslateUnit->setCurrentIndex(0); ui->comboBoxTranslateUnit->blockSignals(false); - int minTranslate = -1000; - int maxTranslate = 1000; + const int minTranslate = -1000; + const int maxTranslate = 1000; ui->doubleSpinBoxCurrentPieceBoxPositionX->setMinimum( UnitConvertor(minTranslate, Unit::Cm, m_oldPieceTranslationUnit)); @@ -791,7 +792,7 @@ void VPMainWindow::InitPropertyTabCurrentPiece() ui->doubleSpinBoxCurrentPieceBoxPositionY->setValue(0); connect(ui->comboBoxTranslateUnit, QOverload::of(&QComboBox::currentIndexChanged), this, - [this, minTranslate, maxTranslate]() + [this V_LAMBDA_CONSTANTS(minTranslate, maxTranslate)]() { const Unit newUnit = TranslateUnit(); const qreal oldTranslateX = ui->doubleSpinBoxCurrentPieceBoxPositionX->value(); diff --git a/src/app/valentina/dialogs/vwidgetbackgroundimages.cpp b/src/app/valentina/dialogs/vwidgetbackgroundimages.cpp index c9182f036..c300dfed2 100644 --- a/src/app/valentina/dialogs/vwidgetbackgroundimages.cpp +++ b/src/app/valentina/dialogs/vwidgetbackgroundimages.cpp @@ -42,6 +42,7 @@ #include "../vtools/undocommands/image/scalebackgroundimage.h" #include "../vtools/undocommands/image/resetbackgroundimage.h" #include "../vmisc/vabstractapplication.h" +#include "../vmisc/lambdaconstants.h" #include #include @@ -844,7 +845,7 @@ void VWidgetBackgroundImages::InitImageTranslation() ui->doubleSpinBoxImageVerticalTranslate->setValue(0); connect(ui->comboBoxTranslateUnit, QOverload::of(&QComboBox::currentIndexChanged), this, - [this, minTranslate, maxTranslate]() + [this V_LAMBDA_CONSTANTS(minTranslate, maxTranslate)]() { const Unit newUnit = CurrentTranslateUnit(); const qreal oldTranslateX = ui->doubleSpinBoxImageHorizontalTranslate->value(); @@ -891,7 +892,7 @@ void VWidgetBackgroundImages::InitImageTranslation() ui->doubleSpinBoxScaleHeight->setValue(100); connect(ui->comboBoxScaleUnit, QOverload::of(&QComboBox::currentIndexChanged), this, - [this, minScale, maxScale]() + [this V_LAMBDA_CONSTANTS(minScale, maxScale)]() { const enum ScaleUnit newUnit = CurrentScaleUnit(); const qreal oldScaleWidth = ui->doubleSpinBoxScaleWidth->value(); diff --git a/src/libs/vmisc/lambdaconstants.h b/src/libs/vmisc/lambdaconstants.h new file mode 100644 index 000000000..d21a05d97 --- /dev/null +++ b/src/libs/vmisc/lambdaconstants.h @@ -0,0 +1,68 @@ +/************************************************************************ + ** + ** @file lambdaconstants.h + ** @author Roman Telezhynskyi + ** @date 29 1, 2022 + ** + ** @brief + ** @copyright + ** This source code is part of the Valentina project, a pattern making + ** program, whose allow create and modeling patterns of clothing. + ** Copyright (C) 2022 Valentina project + ** All Rights Reserved. + ** + ** Valentina is free software: you can redistribute it and/or modify + ** it under the terms of the GNU General Public License as published by + ** the Free Software Foundation, either version 3 of the License, or + ** (at your option) any later version. + ** + ** Valentina is distributed in the hope that it will be useful, + ** but WITHOUT ANY WARRANTY; without even the implied warranty of + ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + ** GNU General Public License for more details. + ** + ** You should have received a copy of the GNU General Public License + ** along with Valentina. If not, see . + ** + *************************************************************************/ +#ifndef LAMBDACONSTANTS_H +#define LAMBDACONSTANTS_H + +/* + given a lambda that wants to capture two constants + + const auto k1 = 1000; + const auto k2 = 2000; + int v = 0; + + auto lambda = [&v, &k1, &k2]() { + v = k1 * k2; + } + + Then unfortunately clang will correctly warn about unnecessary captures. And MSVC will fail to compile if you don't + capture. + + https://stackoverflow.com/questions/52416362/unused-lambda-capture-warning-when-capture-is-actually-used + + An imperfect solution is to declare the lambda using the V_LAMBDA_CONSTANTS macro. + + auto lambda = [&v + V_LAMBDA_CONSTANTS(&k1, &k2) + ](){ + v = k1 * k2; + } + + This should work correctly. Most of the time. + +NOTE: There is no comma after the final capture variable before the V_LAMBDA_CONSTANTS macro. The macro is variadic and +will work with 1 or more captures. +*/ +#ifndef V_LAMBDA_CONSTANTS +#if defined(Q_CC_MSVC) +#define V_LAMBDA_CONSTANTS(...) ,__VA_ARGS__ +#else +#define V_LAMBDA_CONSTANTS(...) +#endif +#endif + +#endif // LAMBDACONSTANTS_H diff --git a/src/libs/vmisc/vmisc.pri b/src/libs/vmisc/vmisc.pri index a326e8aa3..c8541cd0e 100644 --- a/src/libs/vmisc/vmisc.pri +++ b/src/libs/vmisc/vmisc.pri @@ -27,6 +27,7 @@ contains(DEFINES, APPIMAGE) { HEADERS += \ $$PWD/compatibility.h \ + $$PWD/lambdaconstants.h \ $$PWD/stable.h \ $$PWD/def.h \ $$PWD/testpath.h \