Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Cannot register structs/classes with Q_DECLARE_METATYPE
Forum Updated to NodeBB v4.3 + New Features

Cannot register structs/classes with Q_DECLARE_METATYPE

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    markovvv
    wrote on 23 Apr 2024, 18:44 last edited by markovvv
    #1

    Hey guys, i have a little problem.
    When compiling code, i ran into this error:

    /home/*****/work/*****/*****/*****/src/common/common_data/GlobalChannelIdentifier.h:34:1:   required from here
    /usr/include/c++/12/type_traits:1012:52: error: static assertion failed: template argument must be a complete class or an unbounded array
    1012 |       static_assert(std::__is_complete_or_unbounded(__type_identity<_Tp>{}),
    

    Looks like problem is with registering GlobalChannelIdentifier struct.
    Error occurs on tihs line: "Q_DECLARE_METATYPE(GlobalChannelIdentifier)":

    struct GlobalChannelIdentifier : std::pair<std::uintptr_t, int> {
        using pair::pair;
    
        struct Hash {
            std::size_t operator()(const GlobalChannelIdentifier &gi) const noexcept;
        };
    
        friend std::ostream &operator<<(std::ostream &s, const GlobalChannelIdentifier &id);
    };
    
    GlobalChannelIdentifier GetGlobalChannelIdentifier(const GlobalChannelParams &gci) noexcept;
    
    
    Q_DECLARE_METATYPE(GlobalChannelIdentifier)
    

    There are more errors when registering another classes or structs in project.

    Does anybody know, where could be a problem?
    Thank you very much.

    PS.: yes, i have included <QMetaType>

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 23 Apr 2024, 18:52 last edited by SGaist
      #2

      Hi and welcome to devnet,

      I don't have a machine at hand to test but I wonder if you should not use Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE since your type derives from std::pair.

      Or maybe, you might need to also declare a meta type for std::uintptr_t.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply 24 Apr 2024, 07:26
      0
      • S SGaist
        23 Apr 2024, 18:52

        Hi and welcome to devnet,

        I don't have a machine at hand to test but I wonder if you should not use Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE since your type derives from std::pair.

        Or maybe, you might need to also declare a meta type for std::uintptr_t.

        M Offline
        M Offline
        markovvv
        wrote on 24 Apr 2024, 07:26 last edited by markovvv
        #3

        @SGaist Thanks for reply!

        Unfortunately registering std::pair template is not working, teplate is already registered:
        /home/work/*****/src/common/common_data/GlobalChannelIdentifier.h:35:1: error: redefinition of ‘struct QMetaTypeId<std::pair<_T1, _T2> >’
        35 | Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(std::pair)

        And declaring meta type for std::uintptr_t doesnt work out too.
        Could u try the code on your system please? This problem occured while transitioning code from qt5 to qt6.
        Thanks!

        A S 2 Replies Last reply 24 Apr 2024, 18:14
        0
        • M markovvv
          24 Apr 2024, 07:26

          @SGaist Thanks for reply!

          Unfortunately registering std::pair template is not working, teplate is already registered:
          /home/work/*****/src/common/common_data/GlobalChannelIdentifier.h:35:1: error: redefinition of ‘struct QMetaTypeId<std::pair<_T1, _T2> >’
          35 | Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(std::pair)

          And declaring meta type for std::uintptr_t doesnt work out too.
          Could u try the code on your system please? This problem occured while transitioning code from qt5 to qt6.
          Thanks!

          A Offline
          A Offline
          Axel Spoerl
          Moderators
          wrote on 24 Apr 2024, 18:14 last edited by Axel Spoerl
          #4

          @markovvv
          Something else is wrong here, I suspect.
          The code compiles for me (Linux x86_64, Qt 6.7, 6.8)

          Software Engineer
          The Qt Company, Oslo

          1 Reply Last reply
          2
          • M markovvv
            24 Apr 2024, 07:26

            @SGaist Thanks for reply!

            Unfortunately registering std::pair template is not working, teplate is already registered:
            /home/work/*****/src/common/common_data/GlobalChannelIdentifier.h:35:1: error: redefinition of ‘struct QMetaTypeId<std::pair<_T1, _T2> >’
            35 | Q_DECLARE_ASSOCIATIVE_CONTAINER_METATYPE(std::pair)

            And declaring meta type for std::uintptr_t doesnt work out too.
            Could u try the code on your system please? This problem occured while transitioning code from qt5 to qt6.
            Thanks!

            S Offline
            S Offline
            SGaist
            Lifetime Qt Champion
            wrote on 24 Apr 2024, 18:39 last edited by
            #5

            @markovvv
            In addition to @Axel-Spoerl, which distribution are you using ?
            Which version of gcc ?

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            M 1 Reply Last reply 24 Apr 2024, 20:51
            0
            • S SGaist
              24 Apr 2024, 18:39

              @markovvv
              In addition to @Axel-Spoerl, which distribution are you using ?
              Which version of gcc ?

              M Offline
              M Offline
              markovvv
              wrote on 24 Apr 2024, 20:51 last edited by markovvv
              #6

              @SGaist Iam using opensuse Leap 15.5 and version of gcc is 12

              C A 2 Replies Last reply 25 Apr 2024, 00:17
              0
              • M markovvv
                24 Apr 2024, 20:51

                @SGaist Iam using opensuse Leap 15.5 and version of gcc is 12

                C Offline
                C Offline
                ChrisW67
                wrote on 25 Apr 2024, 00:17 last edited by
                #7

                This code compiles as-is for me (Qt 6.6)

                #include <QCoreApplication>
                #include <utility>
                
                struct GlobalChannelParams { }; // faked
                
                struct GlobalChannelIdentifier : std::pair<std::uintptr_t, int> {
                    using pair::pair;
                
                    struct Hash {
                        std::size_t operator()(const GlobalChannelIdentifier &gi) const noexcept;
                    };
                
                    friend std::ostream &operator<<(std::ostream &s, const GlobalChannelIdentifier &id);
                };
                
                GlobalChannelIdentifier GetGlobalChannelIdentifier(const GlobalChannelParams &gci) noexcept;
                
                Q_DECLARE_METATYPE(GlobalChannelIdentifier)
                
                int main(int argc, char **argv) {
                        QCoreApplication app(argc, argv);
                        return 0;
                }
                

                Using either of these:

                g++-11 (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0  // platform default
                g++-12 (Ubuntu 12.3.0-1ubuntu1~22.04) 12.3.0
                

                So this does not seem to be an obvious compiler version issue.

                1 Reply Last reply
                0
                • M markovvv
                  24 Apr 2024, 20:51

                  @SGaist Iam using opensuse Leap 15.5 and version of gcc is 12

                  A Offline
                  A Offline
                  Axel Spoerl
                  Moderators
                  wrote on 25 Apr 2024, 05:03 last edited by
                  #8

                  @markovvv
                  I am on openSuSE Tumbleweed, it compiles with gcc12 and 13.
                  Reading the compile error again, you may wanna check whether #include <utility>is missing or skipped by a pragma. Looks the compiler doesn't know std::pair, when Q_DECLARE_METATYPE is expanded.

                  Software Engineer
                  The Qt Company, Oslo

                  M 1 Reply Last reply 25 Apr 2024, 07:13
                  0
                  • A Axel Spoerl
                    25 Apr 2024, 05:03

                    @markovvv
                    I am on openSuSE Tumbleweed, it compiles with gcc12 and 13.
                    Reading the compile error again, you may wanna check whether #include <utility>is missing or skipped by a pragma. Looks the compiler doesn't know std::pair, when Q_DECLARE_METATYPE is expanded.

                    M Offline
                    M Offline
                    markovvv
                    wrote on 25 Apr 2024, 07:13 last edited by
                    #9

                    Thanks u guys for all of the input! @Axel-Spoerl utility is included. Since iam transfering application from qt5 to qt6, is there any possibility, that there is a qt version collision? Iam out of the ideas, what could be possibly wrong. Application on qt5 compiled fine.

                    A C 2 Replies Last reply 25 Apr 2024, 07:29
                    0
                    • M markovvv
                      25 Apr 2024, 07:13

                      Thanks u guys for all of the input! @Axel-Spoerl utility is included. Since iam transfering application from qt5 to qt6, is there any possibility, that there is a qt version collision? Iam out of the ideas, what could be possibly wrong. Application on qt5 compiled fine.

                      A Offline
                      A Offline
                      Axel Spoerl
                      Moderators
                      wrote on 25 Apr 2024, 07:29 last edited by
                      #10

                      @markovvv

                      Really hard to guess.
                      If out of ideas, I'd start isolating the conflicting behavior into a small reproducer and see whether it happens there. Even if utilities are included and recognized by the code model of your IDE, the compiler might still not see it due to #ifdef'ry or pragma one cascades.

                      Software Engineer
                      The Qt Company, Oslo

                      1 Reply Last reply
                      0
                      • M markovvv
                        25 Apr 2024, 07:13

                        Thanks u guys for all of the input! @Axel-Spoerl utility is included. Since iam transfering application from qt5 to qt6, is there any possibility, that there is a qt version collision? Iam out of the ideas, what could be possibly wrong. Application on qt5 compiled fine.

                        C Offline
                        C Offline
                        ChrisW67
                        wrote on 25 Apr 2024, 08:32 last edited by
                        #11

                        @markovvv Does the self-contained code I posted compile?

                        M 1 Reply Last reply 25 Apr 2024, 11:11
                        0
                        • C ChrisW67
                          25 Apr 2024, 08:32

                          @markovvv Does the self-contained code I posted compile?

                          M Offline
                          M Offline
                          markovvv
                          wrote on 25 Apr 2024, 11:11 last edited by
                          #12

                          @ChrisW67 Yeah, it does

                          Christian EhrlicherC 1 Reply Last reply 25 Apr 2024, 11:20
                          0
                          • M markovvv
                            25 Apr 2024, 11:11

                            @ChrisW67 Yeah, it does

                            Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 25 Apr 2024, 11:20 last edited by
                            #13

                            @markovvv said in Cannot register structs/classes with Q_DECLARE_METATYPE:

                            Yeah, it does

                            So it has neither something to do with the Qt version nor with the compiler but with your code (organisation) - reduce your code until the error is gone and you found the problem.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            1

                            1/13

                            23 Apr 2024, 18:44

                            • Login

                            • Login or register to search.
                            1 out of 13
                            • First post
                              1/13
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved