Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Does C++17 -> "std::any" conflict with QT's control class?



    • Std::any var = std::make_any<QPushButton>("123123", Q_NULLPTR);
      As the example above, no matter how I write, the compiler will report an error as follows:
    1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.16.27023\include\any(471): error C2440: "Initialization": Unable to convert from "initializer list" Is "std::any"
    1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.16.27023\include\any(470): note: no constructor can accept source types, or constructor weights Uncertain decision
    Note: See the reference to the function template being compiled. "std::any std::make_any<QPushButton, const char(&)[7], nullptr>(const char (&)[7], nullptr &&)"
    
    • May I ask how to solve it, thank you very much for your international friends.

  • Lifetime Qt Champion

    Hi @X-Crack, welcome

    • which compiler is this? is it C++17 capable?
    • do you have CONFIG+=C++1z in your .pro file?

    Regards



  • @aha_1980
    My development environment:Visual Studio 2017 15.9.3
    fully supported C++17 is used.
    Sorry, forgive me for being a newcomer, only 600 seconds to post a topic.


  • Lifetime Qt Champion

    Hi,

    Based on this Visual C++ Team Blog post, std::any requires a copyable type which QObject based classes are not.

    Furthermore, QPushButton doesn't have a constructor which takes an initialiser list.


  • Moderators

    @X-Crack well, the template is template< class T, class... Args >

    therefore shouldn't that be

    Std::any var = std::make_any<QPushButton, QString, QObject>("123123", Q_NULLPTR);

    ?



  • @SGaist
    I understand, thank you very much.
    But I am now experiencing a new situation.
    Cause: After using the smart pointer in the standard library, the program crashed when it exited. I think it might have been "delete" twice.

    Std::unordered_map<std::string, std::shared_ptr<QObject>>

    This is what I wrote. I want to add more control classes to the map and then manage it.
    The reality is cruel, and the problems come one after another.
    I am very upset now.


  • Moderators

    @X-Crack said in Does C++17 -> "std::any" conflict with QT's control class?:

    Cause: After using the smart pointer in the standard library, the program crashed when it exited. I think it might have been "delete" twice.

    ... std::shared_ptr<QObject>

    This is what I wrote. I want to add more control classes to the map and then manage it.

    QObject already does some automatic memory management: https://doc.qt.io/qt-5/objecttrees.html

    It is dangerous to use std::shared_ptr with QObject because double deletions can happen very easily. It is safer to store raw pointers (QObject*) in your map -- remember to delete the top-level object when you have finished.

    The reality is cruel, and the problems come one after another.
    I am very upset now.

    加油!



  • @JKSH

    thank you very much.
    Sincerely thank the foreign friends.
    Although I can only rely on "GOOGLE" translation to communicate with you.
    But some words are not standard and you can understand.
    I have basically understood the memory and pointer characteristics in QT.


  • Moderators

    @X-Crack You're welcome. If we say something that you don't understand, just ask us to explain more.



  • @JKSH

    I understand, thank you.
    I changed the way I realized the previous idea.
    Just redesigned


Log in to reply