0

I have the following helper utility class defined in a header:

Util.h

struct Timer
{
    std::chrono::time_point< std::chrono::steady_clock > start { std::chrono::steady_clock::now( ) };
    std::chrono::time_point< std::chrono::steady_clock > end;

    Timer( ) = default;
    ~Timer( )
    {
        end = std::chrono::steady_clock::now( );
        std::clog << "\nTimer took " << std::chrono::duration< double, std::milli >( end - start ).count( ) << " ms\n";
    }
};

Any instance of this struct is used for measuring the execution time of any arbitrary scope (using RAII since the instance gets destroyed at the end of the scope).

However, I don't use the copy/move operations of this struct.
And SonarLint shows a message: "Explicitly define or delete the missing copy constructor and copy assignment operator so that they will not be implicitly provided..."

What's the best practice when it comes to such classes? Should I delete both with = delete? What will happen to move ctor and move operator? Should I also delete them cause I'm not using them right now? Should such a class be non-copyable, non-movable?

digito_evo
  • 3,216
  • 2
  • 14
  • 42
  • Delete the copy operations, and the move ones will be deleted automatically, see https://stackoverflow.com/a/63802083/2752075 – HolyBlackCat Jan 13 '22 at 07:12
  • *Should such a class be non-copyable, non-movable?* That's really up to you to decide according to usage in your code base. Does it make sense to copy or move such an object? – Adrian Mole Jan 13 '22 at 07:13
  • @HolyBlackCat So according to that image, the 4th row describes the situation with my struct, right? And I just need to delete the copy operation? So SonarLint is correct. – digito_evo Jan 13 '22 at 07:20
  • @Adrian Mole I think that those operations have no purpose in this class. So I said maybe I should delete them. – digito_evo Jan 13 '22 at 07:22

0 Answers0