From a8cc6872c3d19fa69fbba610cc865a7697104492 Mon Sep 17 00:00:00 2001 From: Igor Krivenko Date: Mon, 13 May 2024 16:02:12 +0200 Subject: [PATCH] clang-tidy: Address `cppcoreguidelines-missing-std-forward` --- include/libcommute/expression/expression.hpp | 7 ++++- include/libcommute/expression/factories.hpp | 10 +++---- .../libcommute/expression/factories_dyn.hpp | 10 +++---- include/libcommute/expression/monomial.hpp | 11 ++++--- .../libcommute/loperator/hilbert_space.hpp | 5 ++-- include/libcommute/loperator/loperator.hpp | 3 +- .../loperator/mapped_basis_view.hpp | 9 +++--- .../loperator/n_fermion_sector_view.hpp | 29 ++++++++++--------- .../libcommute/loperator/space_partition.hpp | 2 +- .../loperator/sparse_state_vector.hpp | 10 ++++--- include/libcommute/loperator/state_vector.hpp | 3 +- .../loperator/state_vector_eigen3.hpp | 5 ++-- include/libcommute/utility.hpp | 12 ++++++-- test/n_fermion_multisector_view.cpp | 4 +-- 14 files changed, 72 insertions(+), 48 deletions(-) diff --git a/include/libcommute/expression/expression.hpp b/include/libcommute/expression/expression.hpp index ba33faf..6b57b71 100644 --- a/include/libcommute/expression/expression.hpp +++ b/include/libcommute/expression/expression.hpp @@ -25,6 +25,7 @@ #include #include #include +#include #include namespace libcommute { @@ -178,7 +179,7 @@ template class expression { expression_t res; auto& res_mons = res.get_monomials(); for(auto const& m : expr.monomials_) { - auto val = f(m.first, m.second); + auto val = std::forward(f)(m.first, m.second); if(!scalar_traits::is_zero(val)) res_mons.emplace_hint(res_mons.end(), m.first, val); } @@ -741,6 +742,7 @@ template class expression { // Multiplication by scalar (postfix form) // mul_type == ScalarType template + // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) inline expression mul_const_postfix_impl(S&& alpha, std::true_type) const { expression res(*this); for(auto& p : res.monomials_) @@ -752,6 +754,7 @@ template class expression { // mul_type != ScalarType template inline expression_t> + // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) mul_const_postfix_impl(S&& alpha, std::false_type) const { expression_t> res; auto& res_mons = res.get_monomials(); @@ -763,6 +766,7 @@ template class expression { // Multiplication by scalar (prefix form) // mul_type == ScalarType template + // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) inline expression mul_const_prefix_impl(S&& alpha, std::true_type) const { expression res(*this); for(auto& p : res.monomials_) @@ -774,6 +778,7 @@ template class expression { // mul_type != ScalarType template inline expression_t> + // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) mul_const_prefix_impl(S&& alpha, std::false_type) const { expression_t> res; auto& res_mons = res.get_monomials(); diff --git a/include/libcommute/expression/factories.hpp b/include/libcommute/expression/factories.hpp index f000e67..25e3d0d 100644 --- a/include/libcommute/expression/factories.hpp +++ b/include/libcommute/expression/factories.hpp @@ -60,7 +60,7 @@ DEFINE_FACTORY(c_dag, (static_indices::make_fermion(true, INDICES))) // Annihilation operator DEFINE_FACTORY(c, (static_indices::make_fermion(false, INDICES))) // Number of fermions -DEFINE_FACTORY(n, +DEFINE_FACTORY(n, // NOLINT(cppcoreguidelines-missing-std-forward) (static_indices::make_fermion(true, indices...)), (static_indices::make_fermion(false, indices...))) @@ -109,26 +109,26 @@ DEFINE_FACTORY_SPIN(S_z, template inline expression, c_str_to_string_t...> -S_x(IndexTypes&&... indices) { +S_x(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward) return std::complex(0.5) * (static_indices::S_p(indices...) + static_indices::S_m(indices...)); } template inline expression, c_str_to_string_t...> -S_y(IndexTypes&&... indices) { +S_y(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward) return std::complex(0, -0.5) * (static_indices::S_p(indices...) - static_indices::S_m(indices...)); } template inline expression, c_str_to_string_t...> -S_x(IndexTypes&&... indices) { +S_x(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward) return std::complex(0.5) * (static_indices::S_p(indices...) + static_indices::S_m(indices...)); } template inline expression, c_str_to_string_t...> -S_y(IndexTypes&&... indices) { +S_y(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward) return std::complex(0, -0.5) * (static_indices::S_p(indices...) - static_indices::S_m(indices...)); diff --git a/include/libcommute/expression/factories_dyn.hpp b/include/libcommute/expression/factories_dyn.hpp index e09677b..d95651e 100644 --- a/include/libcommute/expression/factories_dyn.hpp +++ b/include/libcommute/expression/factories_dyn.hpp @@ -62,7 +62,7 @@ DEFINE_FACTORY(c_dag, (dynamic_indices::make_fermion(true, INDICES))) // Annihilation operator DEFINE_FACTORY(c, (dynamic_indices::make_fermion(false, INDICES))) // Number of fermions -DEFINE_FACTORY(n, +DEFINE_FACTORY(n, // NOLINT(cppcoreguidelines-missing-std-forward) (dynamic_indices::make_fermion(true, indices...)), (dynamic_indices::make_fermion(false, indices...))) @@ -112,26 +112,26 @@ DEFINE_FACTORY_SPIN(S_z, template inline expression, dyn_indices> -S_x(IndexTypes&&... indices) { +S_x(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward) return std::complex(0.5) * (dynamic_indices::S_p(indices...) + dynamic_indices::S_m(indices...)); } template inline expression, dyn_indices> -S_y(IndexTypes&&... indices) { +S_y(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward) return std::complex(0, -0.5) * (dynamic_indices::S_p(indices...) - dynamic_indices::S_m(indices...)); } template inline expression, dyn_indices> -S_x(IndexTypes&&... indices) { +S_x(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward) return std::complex(0.5) * (dynamic_indices::S_p(indices...) + dynamic_indices::S_m(indices...)); } template inline expression, dyn_indices> -S_y(IndexTypes&&... indices) { +S_y(IndexTypes&&... indices) { // NOLINT(cppcoreguidelines-missing-std-forward) return std::complex(0, -0.5) * (dynamic_indices::S_p(indices...) - dynamic_indices::S_m(indices...)); diff --git a/include/libcommute/expression/monomial.hpp b/include/libcommute/expression/monomial.hpp index 2fee45e..b9d0ba4 100644 --- a/include/libcommute/expression/monomial.hpp +++ b/include/libcommute/expression/monomial.hpp @@ -41,7 +41,8 @@ template class monomial { template void constructor_impl(GenType1&& generator, GenTypesTail&&... more_gens) { using gen1_t = typename std::remove_reference::type; - generators_.emplace_back(make_unique(generator)); + generators_.emplace_back( + make_unique(std::forward(generator))); constructor_impl(std::forward(more_gens)...); } void constructor_impl() {} @@ -211,7 +212,7 @@ template class monomial { friend monomial concatenate(PartTypes&&... parts) { monomial res; res.generators_.reserve(concat_parts_total_size(parts...)); - res.concat_impl(parts...); + res.concat_impl(std::forward(parts)...); return res; } @@ -286,10 +287,12 @@ template class monomial { // Append generators from a mixed list of monomials and monomial ranges template void concat_impl(P1&& p1, PTail&&... p_tail) { - append(p1); + append(std::forward(p1)); concat_impl(std::forward(p_tail)...); } - template void concat_impl(P1&& p1) { append(p1); } + template void concat_impl(P1&& p1) { + append(std::forward(p1)); + } std::vector generators_; }; diff --git a/include/libcommute/loperator/hilbert_space.hpp b/include/libcommute/loperator/hilbert_space.hpp index 15d1f6c..803f3c6 100644 --- a/include/libcommute/loperator/hilbert_space.hpp +++ b/include/libcommute/loperator/hilbert_space.hpp @@ -127,7 +127,8 @@ template class hilbert_space { ESConstructor&& es_constr = {}) { for(auto const& m : expr) { for(auto const& g : m.monomial) { - elementary_spaces_.emplace(es_constr(g), bit_range_t(0, 0)); + elementary_spaces_.emplace(std::forward(es_constr)(g), + bit_range_t(0, 0)); } } recompute_bit_ranges(); @@ -234,7 +235,7 @@ template class hilbert_space { inline friend void foreach(hilbert_space const& hs, Functor&& f) { sv_index_type d = hs.dim(); for(sv_index_type i = 0; i < d; ++i) { - f(i); + std::forward(f)(i); } } diff --git a/include/libcommute/loperator/loperator.hpp b/include/libcommute/loperator/loperator.hpp index 5fc1c4d..ca88fc3 100644 --- a/include/libcommute/loperator/loperator.hpp +++ b/include/libcommute/loperator/loperator.hpp @@ -123,13 +123,14 @@ class loperator : public loperator_base { private: // Implementation details of operator() template + // NOLINTNEXTLINE(cppcoreguidelines-missing-std-forward) inline void act_impl(SrcStateVector&& src, DstStateVector&& dst) const { // A workaround for GCC Bug 58972 // (base::m_actions() would be inaccessible from within the lambda below) auto const& m_act = base::m_actions(); - foreach(src, + foreach(std::forward(src), [&](sv_index_type in_index, element_type_t> const& a) { for(auto const& ma : m_act) { diff --git a/include/libcommute/loperator/mapped_basis_view.hpp b/include/libcommute/loperator/mapped_basis_view.hpp index dcfcfea..25d0d9f 100644 --- a/include/libcommute/loperator/mapped_basis_view.hpp +++ b/include/libcommute/loperator/mapped_basis_view.hpp @@ -78,9 +78,10 @@ inline void update_add_element(mapped_basis_view& view, // update_add_element() is not defined for constant views template -inline void update_add_element(mapped_basis_view&, - sv_index_type, - T&&) { +inline void +update_add_element(mapped_basis_view&, + sv_index_type, + T&&) { // NOLINT(cppcoreguidelines-missing-std-forward) static_assert(!std::is_same::value, "update_add_element() is not supported for constant views"); } @@ -119,7 +120,7 @@ inline void foreach(mapped_basis_view const& view, if(scalar_traits::is_zero(a)) continue; else - f(p.first, a); + std::forward(f)(p.first, a); } } diff --git a/include/libcommute/loperator/n_fermion_sector_view.hpp b/include/libcommute/loperator/n_fermion_sector_view.hpp index 1aa8043..7766478 100644 --- a/include/libcommute/loperator/n_fermion_sector_view.hpp +++ b/include/libcommute/loperator/n_fermion_sector_view.hpp @@ -177,7 +177,7 @@ struct for_each_composition { // Apply 'f' to each composition template void operator()(F&& f) const { if(params.N_counted == 0) { - f(lambdas); + std::forward(f)(lambdas); return; } @@ -336,9 +336,10 @@ inline void update_add_element(n_fermion_sector_view& view, // update_add_element() is not defined for constant views template -inline void update_add_element(n_fermion_sector_view&, - sv_index_type, - T&&) { +inline void +update_add_element(n_fermion_sector_view&, + sv_index_type, + T&&) { // NOLINT(cppcoreguidelines-missing-std-forward) static_assert(!std::is_same::value, "update_add_element() is not supported for constant views"); } @@ -366,8 +367,9 @@ inline void set_zeros(n_fermion_sector_view&) { // Apply functor `f` to all index/non-zero amplitude pairs // in the adapted StateVector object template -inline void foreach(n_fermion_sector_view const& view, - Functor&& f) { +inline void +foreach(n_fermion_sector_view const& view, + Functor&& f) { // NOLINT(cppcoreguidelines-missing-std-forward) if(view.M == 0 && view.M_nonfermion == 0) return; auto dim_nonfermion = detail::pow2(view.M_nonfermion); @@ -385,7 +387,7 @@ inline void foreach(n_fermion_sector_view const& view, if(scalar_traits::is_zero(a)) continue; else - f(view.comp_to_index(lambdas, index_nf), a); + std::forward(f)(view.comp_to_index(lambdas, index_nf), a); } }); } @@ -551,7 +553,7 @@ struct for_each_composition_multi { // Apply 'f' to each composition template void operator()(F&& f) const { if(s_min == -1) { // All compositions are empty - f(lambdas); + std::forward(f)(lambdas); return; } @@ -580,7 +582,7 @@ struct for_each_composition_multi { if(s == s_max && static_cast(i + 1) == sector_params[s].N_counted) { - f(lambdas); + std::forward(f)(lambdas); } else { ++i; if(i == static_cast(sector_params[s].N_counted)) { @@ -918,7 +920,7 @@ template inline void update_add_element(n_fermion_multisector_view&, sv_index_type, - T&&) { + T&&) { // NOLINT(cppcoreguidelines-missing-std-forward) static_assert(!std::is_same::value, "update_add_element() is not supported for constant views"); } @@ -947,8 +949,9 @@ inline void set_zeros(n_fermion_multisector_view&) { // Apply functor `f` to all index/non-zero amplitude pairs // in the adapted StateVector object template -inline void foreach(n_fermion_multisector_view const& view, - Functor&& f) { +inline void +foreach(n_fermion_multisector_view const& view, + Functor&& f) { // NOLINT(cppcoreguidelines-missing-std-forward) if(view.sector_params.size() == 0 && view.M_nonmultisector == 0) return; auto dim_nonmultisector = detail::pow2(view.M_nonmultisector); @@ -969,7 +972,7 @@ inline void foreach(n_fermion_multisector_view const& view, if(scalar_traits::is_zero(a)) continue; else - f(view.comp_to_index(lambdas, index_nms), a); + std::forward(f)(view.comp_to_index(lambdas, index_nms), a); } }); } diff --git a/include/libcommute/loperator/space_partition.hpp b/include/libcommute/loperator/space_partition.hpp index cb1a885..55da4cf 100644 --- a/include/libcommute/loperator/space_partition.hpp +++ b/include/libcommute/loperator/space_partition.hpp @@ -300,7 +300,7 @@ class space_partition { // and index of the subspace this basis state belongs to. template friend void foreach(space_partition const& sp, F&& f) { for(sv_index_type n = 0; n < sp.dim(); ++n) { - f(n, sp[n]); + std::forward(f)(n, sp[n]); } } diff --git a/include/libcommute/loperator/sparse_state_vector.hpp b/include/libcommute/loperator/sparse_state_vector.hpp index 324c28f..1d721a4 100644 --- a/include/libcommute/loperator/sparse_state_vector.hpp +++ b/include/libcommute/loperator/sparse_state_vector.hpp @@ -18,6 +18,7 @@ #include #include +#include namespace libcommute { @@ -59,9 +60,10 @@ template class sparse_state_vector { update_add_element(sparse_state_vector& sv, sv_index_type n, T&& value) { auto it = sv.data_.find(n); if(it == sv.data_.end()) { - if(!scalar_traits::is_zero(value)) sv.data_[n] = value; + if(!scalar_traits::is_zero(value)) + sv.data_[n] = std::forward(value); } else { - it->second += value; + it->second += std::forward(value); if(scalar_traits::is_zero(it->second)) sv.data_.erase(it); } } @@ -78,7 +80,7 @@ template class sparse_state_vector { template inline friend void foreach(sparse_state_vector const& sv, Functor&& f) { for(auto const& p : sv.data_) - f(p.first, p.second); + std::forward(f)(p.first, p.second); } // Force removal of all zero amplitudes from the storage @@ -94,7 +96,7 @@ template class sparse_state_vector { // Force removal of all amplitudes meeting a specified criterion template inline void prune(UnaryPredicate&& p) { for(auto it = data_.begin(); it != data_.end();) { - if(p(it->second)) + if(std::forward(p)(it->second)) it = data_.erase(it); else ++it; diff --git a/include/libcommute/loperator/state_vector.hpp b/include/libcommute/loperator/state_vector.hpp index 7fbd18e..21102b0 100644 --- a/include/libcommute/loperator/state_vector.hpp +++ b/include/libcommute/loperator/state_vector.hpp @@ -17,6 +17,7 @@ #include #include +#include #include namespace libcommute { @@ -74,7 +75,7 @@ inline void foreach(std::vector const& sv, Functor&& f) { if(scalar_traits::is_zero(a)) continue; else - f(n, a); + std::forward(f)(n, a); } } diff --git a/include/libcommute/loperator/state_vector_eigen3.hpp b/include/libcommute/loperator/state_vector_eigen3.hpp index 554e909..e202338 100644 --- a/include/libcommute/loperator/state_vector_eigen3.hpp +++ b/include/libcommute/loperator/state_vector_eigen3.hpp @@ -20,6 +20,7 @@ #include #include +#include namespace libcommute { @@ -133,7 +134,7 @@ inline void foreach(Eigen::DenseBase const& sv, Functor&& f) { if(scalar_traits::is_zero(a)) continue; else - f(n, a); + std::forward(f)(n, a); } } @@ -171,7 +172,7 @@ foreach(Eigen::Block const& sv, if(scalar_traits::is_zero(a)) continue; else - f(n, a); + std::forward(f)(n, a); } } diff --git a/include/libcommute/utility.hpp b/include/libcommute/utility.hpp index 93dbb4b..143ef52 100644 --- a/include/libcommute/utility.hpp +++ b/include/libcommute/utility.hpp @@ -41,12 +41,18 @@ template struct c_str_to_string { template struct c_str_to_string { using type = std::string; }; -template <> struct c_str_to_string { using type = std::string; }; -template <> struct c_str_to_string { using type = std::string; }; +template <> struct c_str_to_string { + using type = std::string; +}; +template <> struct c_str_to_string { + using type = std::string; +}; template <> struct c_str_to_string { using type = std::string; }; -template <> struct c_str_to_string { using type = std::string; }; +template <> struct c_str_to_string { + using type = std::string; +}; template using c_str_to_string_t = typename c_str_to_string::type; diff --git a/test/n_fermion_multisector_view.cpp b/test/n_fermion_multisector_view.cpp index a0a1600..d8ffc5e 100644 --- a/test/n_fermion_multisector_view.cpp +++ b/test/n_fermion_multisector_view.cpp @@ -56,7 +56,7 @@ void check_map_index(view_type const& view, sv_index_type expected_multisector_size) { std::vector mapped_indices; for(sv_index_type index = 0; index < hs.dim(); ++index) { - if(selector(index)) { + if(std::forward(selector)(index)) { mapped_indices.push_back(view.map_index(index)); } } @@ -78,7 +78,7 @@ build_basis_states_ref(hs_type const& hs, std::vector basis_states( n_fermion_multisector_size(hs, sectors)); for(sv_index_type index = 0; index < hs.dim(); ++index) { - if(selector(index)) { + if(std::forward(selector)(index)) { basis_states[view.map_index(index)] = index; } }