sml
sml copied to clipboard
example/visitor.cpp needs weird fix in c++17 mode
Expected Behavior
example/visitor.cpp compiles in c++17 mode
Actual Behavior
it doesn't. In fact it gives very weird template instantiation errors. See e.g. https://stackoverflow.com/q/70941883/85371:
In file included from /data/sml/example/visitor.cpp:8:0:
/data/sml/include/boost/sml.hpp: In instantiation of ‘struct boost::ext::sml::v1_1_4::back::sm_impl<boost::ext::sml::v1_1_4::back::sm_policy<state_name_visitor<boost::ext::sml::v1_1_4::back::sm<boost::ext::sml::v1_1_4::back::sm_policy<composite> > > > >’:
/data/sml/include/boost/sml.hpp:1789:72: required from ‘boost::ext::sml::v1_1_4::back::sm< <template-parameter-1-1> >::operator T&() [with T = state_name_visitor<boost::ext::sml::v1_1_4::back::sm<boost::ext::sml::v1_1_4::back::sm_policy<composite> > >; TSM = boost::ext::sml::v1_1_4::back::sm_policy<composite>]’
/data/sml/example/visitor.cpp:68:62: required from here
/data/sml/include/boost/sml.hpp:1362:68: error: no matching function for call to ‘state_name_visitor<boost::ext::sml::v1_1_4::back::sm<boost::ext::sml::v1_1_4::back::sm_policy<composite> > >::operator()()’
compilation terminated due to -Wfatal-errors.
Steps to Reproduce the Problem
- build example/visitor.cpp in c++17 mode (with e.g. GCC 7.5 but newer versions show the same problem)
- it fails
Specifications
- Version: ANY commit later than 2d25ab5cf2288b240b2b31476e8c4e1a32387eab (git described tags/v1.0.1-413-g2d25ab5)
- Platform: ANY (tested linux)
- Subsystem: ?
The issue reporting template doesn't allow for any constructive information (why). So let me add that here:
-
I tried to bisect the cause. However, that failed, because in the end, the problem originates from long before the visitor.cpp example was ever added to the repository. I could manually pin-point a "last good" revision at 2d25ab5 (the next revision failing with the same compiler error as shown above). So perhaps that commit:
2e07f38 :new: :art: Simplify dependency injection
Holds some kind of answer in case someone understands the workaround below
-
I didn't understand anything about the actual problem - manually tracing template instantiations.
-
However, on a whim I tried to deduce the sm type differently, and lo-and-behold things compile when using expression-decltype instead of variable-decltype:
I cannot say I fully understand this, but the following change makes it work:
const auto state_name = state_name_visitor<decltype(sm)>{sm};
Should be
const auto state_name = state_name_visitor<decltype((sm))>{sm};
Or alternatively
const auto state_name = state_name_visitor<decltype(sm)&>{sm};
Now it compiles in C++17 mode on GCC 7.5.