Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

non virtual destructor #21

Open
ghost opened this issue Jul 19, 2019 · 3 comments
Open

non virtual destructor #21

ghost opened this issue Jul 19, 2019 · 3 comments
Labels

Comments

@ghost
Copy link

ghost commented Jul 19, 2019

Hi,
I like you take on a simple state machine. When building with -Wnon-virtual-dtor you got some warning. I would add
virtual ~MooreMachine() noexcept = default;
to the MooreMachine template.

There is some reason I am missing why it is not there?

@digint
Copy link
Owner

digint commented Jul 19, 2019

Adding a virtual destructor would waste resources, and there is no reason to have one, as the MooreMachine class (and derived classes) is NOT intended to be deleted through a pointer to it.

Or am I missing something?

@ghost
Copy link
Author

ghost commented Jul 19, 2019

If you have already a virtual method which resources are you wasting?

@digint
Copy link
Owner

digint commented Jul 19, 2019

If you have already a virtual method which resources are you wasting?

At least a vtable entry, not sure what else. Building examples/api/moore_machine.cpp results in:

without virtual destructor:

# size -B -d moore_machine
   text    data     bss     dec     hex filename
   3277     768     584    4629    1215 moore_machine

with virtual destructor virtual ~MooreMachine() noexcept = default;

# size -B -d moore_machine
   text    data     bss     dec     hex filename
   3662     808     600    5070    13ce moore_machine

digint added a commit that referenced this issue Jun 11, 2022
Building with `-Wnon-virtual-dtor` prints warnings for each
`tinyfsm::Fsm` class:

> warning: 'class Elevator' has virtual functions and accessible
> non-virtual destructor [-Wnon-virtual-dtor]

Adding a virtual destructor would waste resources (at least one vtable
entry). There is no reason to have a one, as the MooreMachine class
(and derived classes) is NOT intended to be deleted through a pointer
to it.

Building `examples/api/moore_machine.cpp` results in:

- without virtual destructor:

    # size -B -d moore_machine
       text    data     bss     dec     hex filename
       3277     768     584    4629    1215 moore_machine

- with virtual destructor in `MooreMachine`

    # size -B -d moore_machine
       text    data     bss     dec     hex filename
       3662     808     600    5070    13ce moore_machine

Ref: #21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant