Fix warning 'unused-lambda-capture'.

This commit is contained in:
Roman Telezhynskyi 2022-01-29 11:50:13 +02:00
parent 1b188bf414
commit a8aa4fdb7b
4 changed files with 76 additions and 5 deletions

View File

@ -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<int>::of(&QComboBox::currentIndexChanged), this,
[this, minTranslate, maxTranslate]()
[this V_LAMBDA_CONSTANTS(minTranslate, maxTranslate)]()
{
const Unit newUnit = TranslateUnit();
const qreal oldTranslateX = ui->doubleSpinBoxCurrentPieceBoxPositionX->value();

View File

@ -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 <QMenu>
#include <QPushButton>
@ -844,7 +845,7 @@ void VWidgetBackgroundImages::InitImageTranslation()
ui->doubleSpinBoxImageVerticalTranslate->setValue(0);
connect(ui->comboBoxTranslateUnit, QOverload<int>::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<int>::of(&QComboBox::currentIndexChanged), this,
[this, minScale, maxScale]()
[this V_LAMBDA_CONSTANTS(minScale, maxScale)]()
{
const enum ScaleUnit newUnit = CurrentScaleUnit();
const qreal oldScaleWidth = ui->doubleSpinBoxScaleWidth->value();

View File

@ -0,0 +1,68 @@
/************************************************************************
**
** @file lambdaconstants.h
** @author Roman Telezhynskyi <dismine(at)gmail.com>
** @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
** <https://gitlab.com/smart-pattern/valentina> 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 <http://www.gnu.org/licenses/>.
**
*************************************************************************/
#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

View File

@ -27,6 +27,7 @@ contains(DEFINES, APPIMAGE) {
HEADERS += \
$$PWD/compatibility.h \
$$PWD/lambdaconstants.h \
$$PWD/stable.h \
$$PWD/def.h \
$$PWD/testpath.h \