AntelopeIO / appbase

Other
1 stars 6 forks source link

Fix clang compile failure due to use of structure binding #5

Closed linh2931 closed 1 year ago

linh2931 commented 1 year ago

Resolve https://github.com/AntelopeIO/leap/issues/703.

Structured bindings are not capturable by lamda. Use tie instead.

Before the fix:

/home/leap/libraries/appbase/tests/basic_test.cpp:271:48: error: reference to local binding 'pA' declared in enclosing function 'exception_in_exec::test_method'
   app->post(appbase::priority::high, [&] () { pA.do_throw("throwing in pluginA"); });
                                               ^
/home//leap/libraries/appbase/tests/basic_test.cpp:260:10: note: 'pA' declared here
   auto [pA, pB] = plugin_fut.get();
         ^
/home//leap/libraries/appbase/tests/basic_test.cpp:320:48: error: reference to local binding 'pA' declared in enclosing function 'exception_in_shutdown::test_method'
   app->post(appbase::priority::high, [&] () { pA.do_throw("throwing in pluginA"); });
                                               ^
/home//leap/libraries/appbase/tests/basic_test.cpp:309:10: note: 'pA' declared here
   auto [pA, pB] = plugin_fut.get();
         ^
2 errors generated.
greg7mdp commented 1 year ago

Sorry about that. I think it probably could be fixed with (leaving the structured binding):

app->post(appbase::priority::high, [pA = pA] () { pA.do_throw("throwing in pluginA"); });

linh2931 commented 1 year ago

Sorry about that. I think it probably could be fixed with (leaving the structured binding):

app->post(appbase::priority::high, [pA = pA] () { pA.do_throw("throwing in pluginA"); });

Clang does not allow that:

/Users/lh/work/appbase-main/tests/basic_test.cpp:271:40: error: 'pA' in capture list does not name a variable
   app->post(appbase::priority::high, [pA] () { pA.do_throw("throwing in pluginA"); });
                                       ^
/Users/lh/work/appbase-main/tests/basic_test.cpp:271:49: error: reference to local binding 'pA' declared in enclosing function 'exception_in_exec::test_method'
   app->post(appbase::priority::high, [pA] () { pA.do_throw("throwing in pluginA"); });
greg7mdp commented 1 year ago

I had written [pA = pA]. Does clang not allow it?

linh2931 commented 1 year ago

You'll get

/Users/lh/work/appbase-main/tests/basic_test.cpp:271:52: error: 'this' argument to member function 'do_throw' has type 'const pluginA', but function is not marked const
   app->post(appbase::priority::high, [pA=pA] () { pA.do_throw("throwing in pluginA"); });
                                                   ^~
/Users/lh/work/appbase-main/tests/basic_test.cpp:51:13: note: 'do_throw' declared here
   void     do_throw(std::string msg) { throw std::runtime_error(msg); }
greg7mdp commented 1 year ago

OK, thanks for checking Lin! Again sorry I missed this!