Skip to content

Incorrect use of rvalue references and std::forward #315

Description

@jonesmz

See:

grid( DataSet1&& ds1, DataSet2&& ds2 )

Which boils down to

template<typename DataSet1, typename DataSet2>
class grid {
    //! Constructor
    grid( DataSet1&& ds1, DataSet2&& ds2 )
    : m_ds1( std::forward<DataSet1>( ds1 ) )
    , m_ds2( std::forward<DataSet2>( ds2 ) )
    {}
};

Here DataSet1 and DataSet2, while template parameters to the class, are not template parameters to the function.

As a result, this formulation of the constructor always takes these two parameters as rvalue-reference.

This can be corrected in 3 ways

  1. Take both parameters by value
  2. Make the constructor a template function
  3. Make additional constructors that represent the cross-product of const& and && for each parameter. In this case, resulting in 4 functions.

I see that this same mistake is done in more than one place, it's not only grid.hpp where it happens.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions