Code Monkey home page Code Monkey logo

Comments (5)

sylveon avatar sylveon commented on August 18, 2024

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.

ifgotmoney avatar ifgotmoney commented on August 18, 2024

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.

sylveon avatar sylveon commented on August 18, 2024

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.

ifgotmoney avatar ifgotmoney commented on August 18, 2024

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.

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.

sylveon avatar sylveon commented on August 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.