Comments (5)
You can only have one instance of winrt::implements in a class hierarchy. In Bclass
, you have two. Just directly inherit from Aclass (class Bclass : public Aclass
)
from cppwinrt.
You can only have one instance of winrt::implements in a class hierarchy. In
Bclass
, you have two. Just directly inhereit from Aclass (class Bclass : public Aclass
)
I tried this way, but it doesn't work.
my code just works fine with C++17, but not work with C++20, I wonder know if c++/winrt is compatible with C++20;
from cppwinrt.
C++/WinRT is compatible with C++20, what you are seeing is that this code should've been rejected in C++17 mode but wasn't (and in fact Clang does reject it in C++17). This MSVC specific compiler bug was fixed with C++20, so the code is now properly rejected in C++20 mode.
class Bclass : public Aclass
works properly for me. Perhaps we need more context to evaluate why this won't work for you?
Alternatively, you can have only the most derived classes make use of winrt::implements
, or, if you only need weak reference functionality, completely forgo winrt::implements
and use std::shared_ptr
(which can be converted to std::weak_ptr
).
Another alternative is making use of WinRT composition with winrt::composable
and winrt::composing
marker structs, but it's most likely overkill for your use case.
from cppwinrt.
C++/WinRT is compatible with C++20, what you are seeing is that this code should've been rejected in C++17 mode but wasn't (and in fact Clang does reject it in C++17). This MSVC specific compiler bug was fixed with C++20, so the code is now properly rejected in C++20 mode.
class Bclass : public Aclass
works properly for me. Perhaps we need more context to evaluate why this won't work for you?Alternatively, you can have only the most derived classes make use of
winrt::implements
, or, if you only need weak reference functionality, completely forgowinrt::implements
and usestd::shared_ptr
(which can be converted tostd::weak_ptr
).Another alternative is making use of WinRT composition with
winrt::composable
andwinrt::composing
marker structs, but it's most likely overkill for your use case.
thank you for the reply @sylveon .
the reason I want to use winrt::weak_ref
rather than std::weak_ptr
is for the winrt::event
registering. with the winrt::weak_ref
I can simply register an event handler as below:
... m_xxrevoker=eventsource.OnMousePressed(winrt::auto_revoke,{get_weak(), &eventrecipient::eventhandler}) ...
with above code, there will be no lifetime issue of this
pointer in my eventhandler, if using std::shared_ptr
and std::weak_ptr
, the code will be a little bit complicated. my app has dedicated thread to handle mouse input, and the eventhandlers need to be registered and revoked many times, and the user may delete the event recipient any time, so I want to use winrt::weak_ref
to make the code simple as said https://learn.microsoft.com/en-us/windows/uwp/cpp-and-winrt-apis/weak-references.
in my code, the Aclass is actually an abstract class, I wonder what's the right way to write an user-defined interface
just like IInspectable ``IStringable
..etc. should I define Aclass as an interface in an IDL file?
from cppwinrt.
the reason I want to use winrt::weak_ref rather than std::weak_ptr is for the winrt::event registering. with the winrt::weak_ref I can simply register an event handler as below:
You can do the same with std::shared_ptr
, just use weak_from_this()
, see #1330.
Also, if Aclass is strictly an interface, you can define it as such:
MIDL_INTERFACE("SOME-GUID") IAclass : IUnknown
{
virtual void SayHello() = 0;
virtual std::string GetString() = 0; // no need to conform to COM interface restrictions
};
And then do like this
class Bclass : public winrt::implements<Bclass, IAclass>
from cppwinrt.
Related Issues (20)
- Expose IIterator<T> as a sentinel view HOT 1
- Generate empty module.g.cpp HOT 1
- Bug: Visual Studio integration? HOT 3
- Feature: Add support for agile+weak references HOT 1
- Bug: Upgrading from 2.0.221104.6 to a later version no longer builds co_await on a DispatcherQueue HOT 6
- Project interfaces without forwarders to required interfaces HOT 2
- Bug:Since 2.0.240111.5, I get a compile error with the GetTemplateChild function. HOT 5
- Expose wrapped APIs to open Win32 handles for WinRT storage items HOT 2
- GetChildrenInTabFocusOrder no longer a member of the generated code HOT 10
- Cannot use WinRT with MFC? HOT 3
- Bug: version 2.0.240111.5 does not work correctly with XAML HOT 10
- Bug:Build failure due to namespace changes in XamlTypeInfo after C++/WinRT update HOT 2
- Bug: throw_hresult should treat ERROR_COMMITMENT_LIMIT as OOM HOT 3
- Bug: co_await apartment_context crashs with ''CoInitialize has not been called.'' HOT 3
- Bug: Calling PropertyValue.Create to handle boxing is significantly slower than allocating an IReference directly HOT 5
- Bug: projection generator does not handle absolute unix path HOT 2
- Feature: Support C++20 std::format for cppwinrt types (e.g. winrt::hstring) HOT 2
- Bug: DeviceInformation::FindAddAsync returns empty collection when specifying a usb device. HOT 1
- Bug: Weird c++20 MainWindow.xaml.g.hpp bug HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cppwinrt.