-
Notifications
You must be signed in to change notification settings - Fork 247
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
Simply static lifetime implementation #1375
Simply static lifetime implementation #1375
Conversation
Another improvement can be done here - regarding constructors. I sometimes want to write the CreateInstance* functions manually due to have more than one implementation class for the runtime class. |
I can't make sense of what optimizations this does. |
@Link1J
Yes, that is already possible. However, when you use enable component optimisations1, it does not compile because the wrapper code in Class.g.cpp always accesses implementation::Class, implying that the functions must be present on it, which conflicts with static lifetime as you really want to put the static methods as non-static ones on the factory to access the state. Therefore, at the moment you need to manually write forwarder methods as noted here. Footnotes
|
Sorry, I've not had a moment to look at this. |
It feels a little hinky to rely on "call a static method on a null this kind of works..." So you goal is to eliminate the "if you are using component optimizations [which everybody should be] then you will need to write forwarder methods" part? |
Yes, but it seems to be the simplest way to solve this problem. Also, note that there is no undefined behaviour because the method has to be static and C++ allows calling static methods of a type through a pointer or reference of type. |
Actually, it is. |
Okay, but it works so not going to care. It's not like C++/WinRT has no UB already. Hmm actually it is not UB according to this answer: https://stackoverflow.com/a/63332797/12025335. |
That's hard to prove (and by "hard" I mean "impossible"). I will admit that this is almost guaranteed to remain inconsequential with MSVC, given that it has to support MFC sporting its infamous
If that is the case, the reasonable response should be to remove the UB, not to add to it. |
Putting the following does not work, as only one branch is syntactically valid at a given time. Still think the nullptr trick is the best solution here. if constexpr (winrt::impl::has_static_lifetime_v<test_component::factory_implementation::StaticClass>)
{
winrt::make_self<test_component::factory_implementation::StaticClass>()->Method();
}
else
{
test_component::factory_implementation::StaticClass::statics_type::Method(); // could cause an error in case the first branch is the chosen one.
} |
The entire point of if constexpr is allowing the branch not being picked to have invalid code |
All the branches should still be syntactically valid, unless the expressions are based on a template parameter (see these two examples: https://godbolt.org/z/MPc8KjxWd and https://godbolt.org/z/Ye3Txfqoz). In this case I cannot turn those functions into templates, however. Wait I have an idea - use a lambda template! Hmm though it is a C++20 feature. |
…/cppwinrt into static_lifetime
…/cppwinrt into static_lifetime
This pull request is stale because it has been open 10 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
Even without static lifetime, you still may want to put static methods on the factory as static, so that you can have multiple implementations of the implementation type with different names. |
This pull request is stale because it has been open 10 days with no activity. Remove stale label or comment or this will be closed in 5 days. |
Implements #1300.
Please merge this pull request as I really need this feature.