Std::forward inside requires() clause

In my c++20 code I have the following method:

template < typename T, typename P, typename... Args >

    requires std::is_base_of_v< QDialog, T > && std::is_base_of_v< QWidget, P >

             && requires( T, P* parent, 

Args&&... args ) { T( parent, std::forward< Args >( args )... ); }

QPointer< T > makeWithParent( P* parent, Args&&... args ) {

    assert( parent != nullptr);

    QPointer< T > newObject = new T( parent, std::forward< Args >( args )... );

   assert( newObject->parentWidget() == parent);

    return newObject;

  • Which rule?
    Sonarqube flags the line with the requires clause, but not the line in the function body:
    “std::forward” should only be called on a forwarding reference. ( cpp:S5417)

  • Why do you believe it’s a false-positive/false-negative?
    Even though it’s in the requires clause I would still think it’s a forwarding reference.
    (Though I admit that I don’t feel confident with looking that up in the c++ standard…)

  • Which sonarqube version you are using?
    I’m using sonarqube Version 9.9.3

Side note: the method above is meant to deal with the conflict of using sonarqube on Qt-based projects which contain a lot of new operators therefore violate cpp:S5025 “Replace the use of “new” with an operation that automatically manages the memory.” So there are more overloads that deal with QWidgets, QGraphicsItem, etc.

We recently observed that developers set it to false positive for QObject-related types even when the parent ist not set, proving the point of S5025. Would love to hear from other sonarqube users how they deal with that.

Hi @jsinge, thanks for raising this point.

I created a ticket to fix the false positive.

On your side, you may be able to silence the problem by using std::is_constructible_v<T, P*, Args&&...>.

Also regarding Qt and S5025, your approach of using a wrapper seems like the best solution, as far as I know the library.

Thanks! Also for the hint to std::is_constructible_v, looks like a much cleaner solution :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.