compiler complains about deleted functions when declaring union in call

Viewed 51

I am struggling to use a union wrapped in a struct, in a class.

Update

Removed the previous code snippets, I simplified the actual code so that I can copy/paste it in here, with the corresponding compiler errors.

I still have no clue how to fix this. I am adding constructors/destructors/copy-, move-constructors in places that I think the compiler is instructing me to do, but nothing gives.

namespace modules::blocks
{
    struct input 
    {
        // The std::function remains problematic. If I put
        // bool/int/etc it compiles fine. std::function does not
        std::function<void()> a;
    };
}

Then the scenario class with the definition of the union

namespace valinso::modules::blocks
{
    union block_type
    {
        input i;
    };

    struct block_definition
    {
        block_type block;
    };

    class scenario
    {
    public:
        
    private:
        block_definition b;
        // in the end, I need to store them in a vector
        // but to keep it as minimal as possible, even this doesn't compile
        // DO NOTE: when I define the vector, compiler also complains about a static assert that my types are not destructable (while I have no clue why they are not...)
        //etl_vector_20<block_definition> blocks;
    };

This gives the following compiler output

/home/bp/dev/iobox/firmware/unit_tests/source/valinso/modules/interrupt_management/interrupt_manager_tests.cpp: In member function ‘virtual void {anonymous}::interrupt_manager_tests_pin_changed_add_message_to_queue_Test::TestBody()’:
/home/bp/dev/iobox/firmware/unit_tests/source/valinso/modules/interrupt_management/interrupt_manager_tests.cpp:56:35: error: use of deleted function ‘modules::blocks::scenario::scenario()’
   56 |         modules::blocks::scenario a;
      |                                   ^
In file included from /home/bp/dev/iobox/firmware/unit_tests/source/valinso/modules/interrupt_management/interrupt_manager_tests.cpp:5:
/home/bp/dev/iobox/firmware/unit_tests/source/../../valinso/app/modules/blocks/scenario.hpp:31:11: note: ‘modules::blocks::scenario::scenario()’ is implicitly deleted because the default definition would be ill-formed:
   31 |     class scenario
      |           ^~~~~~~~
/home/bp/dev/iobox/firmware/unit_tests/source/../../valinso/app/modules/blocks/scenario.hpp:31:11: error: use of deleted function ‘modules::blocks::block_definition::block_definition()’
/home/bp/dev/iobox/firmware/unit_tests/source/../../valinso/app/modules/blocks/scenario.hpp:26:12: note: ‘modules::blocks::block_definition::block_definition()’ is implicitly deleted because the default definition would be ill-formed:
   26 |     struct block_definition
      |            ^~~~~~~~~~~~~~~~
/home/bp/dev/iobox/firmware/unit_tests/source/../../valinso/app/modules/blocks/scenario.hpp:26:12: error: use of deleted function ‘modules::blocks::block_type::block_type()’
/home/bp/dev/iobox/firmware/unit_tests/source/../../valinso/app/modules/blocks/scenario.hpp:21:11: note: ‘modules::blocks::block_type::block_type()’ is implicitly deleted because the default definition would be ill-formed:
   21 |     union block_type
      |           ^~~~~~~~~~
/home/bp/dev/iobox/firmware/unit_tests/source/../../valinso/app/modules/blocks/scenario.hpp:23:15: error: union member ‘modules::blocks::block_type::i’ with non-trivial ‘modules::blocks::input::input()’
   23 |         input i;
      |               ^
/home/bp/dev/iobox/firmware/unit_tests/source/../../valinso/app/modules/blocks/scenario.hpp:26:12: error: use of deleted function ‘modules::blocks::block_type::~block_type()’
   26 |     struct block_definition
      |            ^~~~~~~~~~~~~~~~
/home/bp/dev/iobox/firmware/unit_tests/source/../../valinso/app/modules/blocks/scenario.hpp:21:11: note: ‘modules::blocks::block_type::~block_type()’ is implicitly deleted because the default definition would be ill-formed:
   21 |     union block_type
      |           ^~~~~~~~~~
/home/bp/dev/iobox/firmware/unit_tests/source/../../valinso/app/modules/blocks/scenario.hpp:23:15: error: union member ‘modules::blocks::block_type::i’ with non-trivial ‘modules::blocks::input::~input()’
   23 |         input i;
      |               ^
/home/bp/dev/iobox/firmware/unit_tests/source/../../valinso/app/modules/blocks/scenario.hpp:31:11: error: use of deleted function ‘modules::blocks::block_definition::~block_definition()’
   31 |     class scenario
      |           ^~~~~~~~
/home/bp/dev/iobox/firmware/unit_tests/source/../../valinso/app/modules/blocks/scenario.hpp:26:12: note: ‘modules::blocks::block_definition::~block_definition()’ is implicitly deleted because the default definition would be ill-formed:
   26 |     struct block_definition
      |            ^~~~~~~~~~~~~~~~
/home/bp/dev/iobox/firmware/unit_tests/source/../../valinso/app/modules/blocks/scenario.hpp:26:12: error: use of deleted function ‘modules::blocks::block_type::~block_type()’
/home/bp/dev/iobox/firmware/unit_tests/source/valinso/modules/interrupt_management/interrupt_manager_tests.cpp:56:35: error: use of deleted function ‘modules::blocks::scenario::~scenario()’
   56 |         modules::blocks::scenario a;
      |                                   ^
In file included from /home/bp/dev/iobox/firmware/unit_tests/source/valinso/modules/interrupt_management/interrupt_manager_tests.cpp:5:
/home/bp/dev/iobox/firmware/unit_tests/source/../../valinso/app/modules/blocks/scenario.hpp:31:11: note: ‘modules::blocks::scenario::~scenario()’ is implicitly deleted because the default definition would be ill-formed:
   31 |     class scenario
      |           ^~~~~~~~
/home/bp/dev/iobox/firmware/unit_tests/source/../../valinso/app/modules/blocks/scenario.hpp:31:11: error: use of deleted function ‘modules::blocks::block_definition::~block_definition()’
gmake[2]: *** [CMakeFiles/unittests.dir/build.make:1000: CMakeFiles/unittests.dir/valinso/modules/interrupt_management/interrupt_manager_tests.cpp.o] Error 1
gmake[2]: *** Waiting for unfinished jobs....

When I change the member in scenario class to a vector of block_definitions then I get this error which I also don't understand.

/home/bp/dev/iobox/firmware/unit_tests/source/../../thirdparty/etl/include/etl/vector.h:1194:23:   required from ‘etl::vector<T, MAX_SIZE_>::vector() [with T = modules::blocks::block_definition; long unsigned int MAX_SIZE_ = 20]’
/home/bp/dev/iobox/firmware/unit_tests/source/../../valinso/app/modules/blocks/scenario.hpp:31:11:   required from here
/usr/include/c++/11/bits/stl_construct.h:188:51: error: static assertion failed: value type is destructible
  188 |       static_assert(is_destructible<_Value_type>::value,
      |                                                   ^~~~~

The problem seems to be with defining an std::function in the struct which is part of the union. I am trying hard to understand what is explained here and here, but whatever I try to fix in the constructors (adding them to make them explicit) won't satisfy the compiler.

I clearly misunderstand something, I hope somebody can explain it in easier terms so that I will understand what the problem + solution is.

0 Answers
Related