gelldur/EventBus

Build Failed, “moving a temporary object prevents copy elision”

SSDGADsss opened this issue · 3 comments

Describe the bug
When I build project on my computer, my gcc give an error
I try release and master branch code, have same problem

In file included from /home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/EventBus.hpp:13,
                 from /home/shenzhe/Code/EventBus-3.0.4/test/unit/src/dexode/eventbus/test/SuiteEventBus.cpp:5:
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp: In instantiation of ‘bool dexode::eventbus::postpone(Bus&, std::any) [with Event = test::event::Value]’:
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:59:41:   required from ‘static dexode::eventbus::PostponeHelper dexode::eventbus::PostponeHelper::create(std::any&&) [with Event = dexode::eventbus::test::event::Value]’
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:82:52:   required from ‘bool dexode::eventbus::Bus::postpone(Event) [with Event = dexode::eventbus::test::event::Value]’
/home/shenzhe/Code/EventBus-3.0.4/test/unit/src/dexode/eventbus/test/SuiteEventBus.cpp:66:14:   required from here
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: error: moving a temporary object prevents copy elision [-Werror=pessimizing-move]
  126 |         return bus.postpone(std::move(std::any_cast<Event>(event)));
      |                ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: note: remove ‘std::move’ call
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp: In instantiation of ‘bool dexode::eventbus::postpone(Bus&, std::any) [with Event = test::event::T1]’:
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:59:41:   required from ‘static dexode::eventbus::PostponeHelper dexode::eventbus::PostponeHelper::create(std::any&&) [with Event = dexode::eventbus::test::event::T1]’
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:82:52:   required from ‘bool dexode::eventbus::Bus::postpone(Event) [with Event = dexode::eventbus::test::event::T1]’
/home/shenzhe/Code/EventBus-3.0.4/test/unit/src/dexode/eventbus/test/SuiteEventBus.cpp:127:15:   required from here
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: error: moving a temporary object prevents copy elision [-Werror=pessimizing-move]
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: note: remove ‘std::move’ call
[ 86%] Built target UseCase_Basic
In file included from /home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/EventBus.hpp:13,
                 from /home/shenzhe/Code/EventBus-3.0.4/test/integration/src/dexode/eventbus/test/SuiteWait.cpp:12:
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp: In instantiation of ‘bool dexode::eventbus::postpone(Bus&, std::any) [with Event = {anonymous}::EventTest]’:
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:59:41:   required from ‘static dexode::eventbus::PostponeHelper dexode::eventbus::PostponeHelper::create(std::any&&) [with Event = {anonymous}::EventTest]’
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:82:52:   required from ‘bool dexode::eventbus::Bus::postpone(Event) [with Event = {anonymous}::EventTest]’
/home/shenzhe/Code/EventBus-3.0.4/test/integration/src/dexode/eventbus/test/SuiteWait.cpp:43:16:   required from here
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: error: moving a temporary object prevents copy elision [-Werror=pessimizing-move]
  126 |         return bus.postpone(std::move(std::any_cast<Event>(event)));
      |                ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: note: remove ‘std::move’ call
In file included from /home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/EventBus.hpp:13,
                 from /home/shenzhe/Code/EventBus-3.0.4/test/unit/src/dexode/eventbus/test/SuiteListener.cpp:5:
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp: In instantiation of ‘bool dexode::eventbus::postpone(Bus&, std::any) [with Event = test::event::Value]’:
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:59:41:   required from ‘static dexode::eventbus::PostponeHelper dexode::eventbus::PostponeHelper::create(std::any&&) [with Event = dexode::eventbus::test::event::Value]’
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:82:52:   required from ‘bool dexode::eventbus::Bus::postpone(Event) [with Event = dexode::eventbus::test::event::Value]’
/home/shenzhe/Code/EventBus-3.0.4/test/unit/src/dexode/eventbus/test/SuiteListener.cpp:25:14:   required from here
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: error: moving a temporary object prevents copy elision [-Werror=pessimizing-move]
  126 |         return bus.postpone(std::move(std::any_cast<Event>(event)));
      |                ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: note: remove ‘std::move’ call
In file included from /home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/EventBus.hpp:13,
                 from /home/shenzhe/Code/EventBus-3.0.4/test/unit/src/dexode/eventbus/test/SuiteConcurrentEventBus.cpp:9:
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp: In instantiation of ‘bool dexode::eventbus::postpone(Bus&, std::any) [with Event = test::SimpleEvent]’:
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:59:41:   required from ‘static dexode::eventbus::PostponeHelper dexode::eventbus::PostponeHelper::create(std::any&&) [with Event = dexode::eventbus::test::SimpleEvent]’
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:82:52:   required from ‘bool dexode::eventbus::Bus::postpone(Event) [with Event = dexode::eventbus::test::SimpleEvent]’
/home/shenzhe/Code/EventBus-3.0.4/test/unit/src/dexode/eventbus/test/SuiteConcurrentEventBus.cpp:44:16:   required from here
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: error: moving a temporary object prevents copy elision [-Werror=pessimizing-move]
  126 |         return bus.postpone(std::move(std::any_cast<Event>(event)));
      |                ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: note: remove ‘std::move’ call
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp: In instantiation of ‘bool dexode::eventbus::postpone(Bus&, std::any) [with Event = test::event::T1]’:
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:59:41:   required from ‘static dexode::eventbus::PostponeHelper dexode::eventbus::PostponeHelper::create(std::any&&) [with Event = dexode::eventbus::test::event::T1]’
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:82:52:   required from ‘bool dexode::eventbus::Bus::postpone(Event) [with Event = dexode::eventbus::test::event::T1]’
/home/shenzhe/Code/EventBus-3.0.4/test/unit/src/dexode/eventbus/test/SuiteListener.cpp:26:14:   required from here
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: error: moving a temporary object prevents copy elision [-Werror=pessimizing-move]
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: note: remove ‘std::move’ call
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp: In instantiation of ‘bool dexode::eventbus::postpone(Bus&, std::any) [with Event = test::event::WaitPerk]’:
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:59:41:   required from ‘static dexode::eventbus::PostponeHelper dexode::eventbus::PostponeHelper::create(std::any&&) [with Event = dexode::eventbus::test::event::WaitPerk]’
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:82:52:   required from ‘bool dexode::eventbus::Bus::postpone(Event) [with Event = dexode::eventbus::test::event::WaitPerk]’
/home/shenzhe/Code/EventBus-3.0.4/test/unit/src/dexode/eventbus/test/SuiteConcurrentEventBus.cpp:145:16:   required from here
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: error: moving a temporary object prevents copy elision [-Werror=pessimizing-move]
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: note: remove ‘std::move’ call
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp: In instantiation of ‘bool dexode::eventbus::postpone(Bus&, std::any) [with Event = test::event::T1]’:
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:59:41:   required from ‘static dexode::eventbus::PostponeHelper dexode::eventbus::PostponeHelper::create(std::any&&) [with Event = dexode::eventbus::test::event::T1]’
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:82:52:   required from ‘bool dexode::eventbus::Bus::postpone(Event) [with Event = dexode::eventbus::test::event::T1]’
/home/shenzhe/Code/EventBus-3.0.4/test/unit/src/dexode/eventbus/test/SuiteConcurrentEventBus.cpp:146:16:   required from here
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: error: moving a temporary object prevents copy elision [-Werror=pessimizing-move]
/home/shenzhe/Code/EventBus-3.0.4/lib/src/dexode/eventbus/Bus.hpp:126:28: note: remove ‘std::move’ call

How should I fix this error

Build:

  • compiler: GCC
  • Version: 13.2.1
  • System: Archlinux
  • Arch: x86_64

Hello, I will take a look at this. Thanks for the issue. Could you provide your example struct (event) that this issue occurs ?
What error flags do you use to compile? -Wall ?

Now I see that issue is already in test code. I have same issue so I will fix. Thanks. Need to improve CI.

Hey @SSDGADsss, I fixed and tested on

  • gcc 13.2.1 20230801
  • clang 16.0.6

Checkout development branch. It is stable as I'm using it daily. Need to find some time and improve overall.