c++destructorrule-of-threerule-of-five

C++ - clang tidy complain about rule of X?


This code raises a warning in clang tidy:

Class 'Locker' defines a non-default destructor but does not define a copy constructor, a copy assignment operator, a move constructor or a move assignment operatorclang-tidy(cppcoreguidelines-special-member-functions)

This is the updated struct, according to the comments:

struct Locker
{
    std::binary_semaphore *sem = nullptr;

    // ----------------------------------
    // Methods
    // ----------------------------------
    auto try_lock(std::binary_semaphore &sem_, u32 time_in_seconds = 1) -> bool;

    auto manual_release() -> void;

    // ----------------------------------
    // Deleted
    // ----------------------------------

    Locker(Locker &) = delete;

    Locker(Locker &&) = delete;

    Locker(std::binary_semaphore &&sem_) noexcept = delete;

    Locker(std::binary_semaphore &sem_) noexcept = delete;

    Locker(std::binary_semaphore *sem_) noexcept = delete;

    Locker() noexcept = default;

    auto operator=(std::binary_semaphore &sem_) noexcept -> Locker & = delete;

    auto operator=(std::binary_semaphore &&sem_) noexcept -> Locker & = delete;

    auto operator=(std::binary_semaphore *sem_) noexcept -> Locker & = delete;

    // ----------------------------------
    // Destructor
    // ----------------------------------
    ~Locker()
    {

        manual_release();
    }
};

I don't want any constructors but I want a specific destructor. we have method to try lock and the destructor just release the lock cleanly making sure everything is ok.

Please note that GCC 12.2 with

-Wall -Wextra -pedantic -pedantic-errors -Werror -Wuninitialized -Wtrivial-auto-var-init -Wshadow -Wnormalized -Wno-error=comment

doesn't even bother.

How to suppress that warning?

Thanks!

Edit:

Below is the godbolt link to replicate the situation, may someone correct my code and share the link here? Link to clang-tidy struct issue


Solution

  • Mostly by help from Aconcagua and n.m. we found out what your misconception was.

    This has the wrong signature:

    Locker( Locker &) = delete;
    

    Copy constructor takes a const Locker&. And you do not delete the assignment. Those are the five:

    Locker(const Locker &) = delete;
    Locker(Locker &&) = delete;
    Locker& operator=(const Locker&) = delete;
    Locker& operator=(Locker&&) = delete;
    ~Locker() { 
        std::cout << "";
     }
    

    If you explicitly declare them all, the warning should be gone: https://godbolt.org/z/eP8TrE9b1