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. Throwing a C++ exception instead of returning a default-constructed object by QVariant::value()
QtWS25 Last Chance

Throwing a C++ exception instead of returning a default-constructed object by QVariant::value()

Scheduled Pinned Locked Moved Unsolved General and Desktop
qvariantvaluesc++ exceptionsconstructionconversions
14 Posts 3 Posters 3.4k Views
  • 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.
  • E elfring

    If you want to avoid the cost of default constructed object …

    I know the possibility of the check by the function “QVariant::canConvert”.

    • But I became concerned that this can not be as efficient as using a variant of the function “QVariant::value” because a QVariant object needs to be created just for this function call.
    • I imagine that the desired data type conversion can become a bit safer by the other way.
    Chris KawaC Offline
    Chris KawaC Offline
    Chris Kawa
    Lifetime Qt Champion
    wrote on last edited by Chris Kawa
    #4

    I became concerned that this can not be as efficient as using a variant of the function “QVariant::value” because a QVariant object needs to be created just for this function call.

    Sorry, I don't understand what you mean. You call value() on an object so you already have an object. How is calling canConvert() on the same object first not as efficient? Can you maybe give a code snippet example of what you mean?

    I imagine that the desired data type conversion can become a bit safer by the other way.

    How so? Can you give an example of such case?

    E 2 Replies Last reply
    1
    • Chris KawaC Chris Kawa

      I became concerned that this can not be as efficient as using a variant of the function “QVariant::value” because a QVariant object needs to be created just for this function call.

      Sorry, I don't understand what you mean. You call value() on an object so you already have an object. How is calling canConvert() on the same object first not as efficient? Can you maybe give a code snippet example of what you mean?

      I imagine that the desired data type conversion can become a bit safer by the other way.

      How so? Can you give an example of such case?

      E Offline
      E Offline
      elfring
      wrote on last edited by
      #5

      How is calling canConvert() on the same object first not as efficient?

      I would repeat a data type check which can be performed by the Qt software library already.

      Chris KawaC 1 Reply Last reply
      0
      • Chris KawaC Chris Kawa

        I became concerned that this can not be as efficient as using a variant of the function “QVariant::value” because a QVariant object needs to be created just for this function call.

        Sorry, I don't understand what you mean. You call value() on an object so you already have an object. How is calling canConvert() on the same object first not as efficient? Can you maybe give a code snippet example of what you mean?

        I imagine that the desired data type conversion can become a bit safer by the other way.

        How so? Can you give an example of such case?

        E Offline
        E Offline
        elfring
        wrote on last edited by
        #6

        Can you give an example of such case?

        Are further software development considerations relevant for data synchronisation around mentioned source code places?

        1 Reply Last reply
        0
        • E elfring

          How is calling canConvert() on the same object first not as efficient?

          I would repeat a data type check which can be performed by the Qt software library already.

          Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #7

          I would repeat a data type check which can be performed by the Qt software library already.

          Yes, but those are nano-optimizations that are not usually relevant. Have you measured that it has a real negative impact on performance of your app?
          Do you consider throwing an exception and related stack unwinding a cheaper operation than comparing types (which sometimes optimizes to comparing two ints)? Have you measured that one is significantly faster than another on your target architecture?

          Are further software development considerations relevant for data synchronisation around mentioned source code places?

          I'm not sure what you're asking. Do you mean is QVariant thread-safe? It's not. You need to do thread synchronization on your side.

          E 1 Reply Last reply
          1
          • Chris KawaC Chris Kawa

            I would repeat a data type check which can be performed by the Qt software library already.

            Yes, but those are nano-optimizations that are not usually relevant. Have you measured that it has a real negative impact on performance of your app?
            Do you consider throwing an exception and related stack unwinding a cheaper operation than comparing types (which sometimes optimizes to comparing two ints)? Have you measured that one is significantly faster than another on your target architecture?

            Are further software development considerations relevant for data synchronisation around mentioned source code places?

            I'm not sure what you're asking. Do you mean is QVariant thread-safe? It's not. You need to do thread synchronization on your side.

            E Offline
            E Offline
            elfring
            wrote on last edited by
            #8

            Yes, but those are nano-optimizations that are not usually relevant.

            I try to avoid questionable checks a bit more.

            Do you consider throwing an exception and related stack unwinding a cheaper operation than comparing types …?

            I have got other software development preferences here. I would like to use the C++ exception handling system in the way it was designed.
            I became curious if a statement like “throw data_type_conversion_failure();” can be used instead of “return T();”.

            You need to do thread synchronization on your side.

            Will this eventually be provided by a companion class template library?

            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #9

              I would like to use the C++ exception handling system in the way it was designed.

              That's fine. Qt mostly doesn't use that programming style so if it's important to you you should look for alternatives e.g. std::variant. There are dozens other variant implementations out there or you can roll your own if none of them behave exactly like you want. I'm pretty sure Qt is not gonna change how it handles errors. It's decades too late for that.

              Qt was born in times when exception handling was either unavailable or not equally featured on all supported platforms so not using them was an obvious choice for a cross-platform library design. Today there would be no benefit in converting such a huge library to another programming paradigm just for the sake of it. There are also real world considerations, like a notable library size increase when turning exceptions on (as mentioned here).

              Will this eventually be provided by a companion class template library?

              I'm again not sure what you're asking. A thread safe access to QObject? I don't see how that could be buit-in in a generic enough way as to not incur a big size and performance hit. It's more of a case by case thing and I doubt Qt will provide anything like that. It's easy enough to create a thread-safe wrappers around your QObjects tailored to your own needs. Qt does provide the necessary primitives like QMutex or QReadWriteLock for that.

              But it's an open project so if someone submits a library for inclusion then who knows, maybe. I'm not aware of any such work being done or planned though.

              E 1 Reply Last reply
              2
              • Chris KawaC Chris Kawa

                I would like to use the C++ exception handling system in the way it was designed.

                That's fine. Qt mostly doesn't use that programming style so if it's important to you you should look for alternatives e.g. std::variant. There are dozens other variant implementations out there or you can roll your own if none of them behave exactly like you want. I'm pretty sure Qt is not gonna change how it handles errors. It's decades too late for that.

                Qt was born in times when exception handling was either unavailable or not equally featured on all supported platforms so not using them was an obvious choice for a cross-platform library design. Today there would be no benefit in converting such a huge library to another programming paradigm just for the sake of it. There are also real world considerations, like a notable library size increase when turning exceptions on (as mentioned here).

                Will this eventually be provided by a companion class template library?

                I'm again not sure what you're asking. A thread safe access to QObject? I don't see how that could be buit-in in a generic enough way as to not incur a big size and performance hit. It's more of a case by case thing and I doubt Qt will provide anything like that. It's easy enough to create a thread-safe wrappers around your QObjects tailored to your own needs. Qt does provide the necessary primitives like QMutex or QReadWriteLock for that.

                But it's an open project so if someone submits a library for inclusion then who knows, maybe. I'm not aware of any such work being done or planned though.

                E Offline
                E Offline
                elfring
                wrote on last edited by
                #10

                A thread safe access to QObject?

                A few programming interfaces are thread-safe at the moment.

                I don't see how that could be buit-in in a generic enough way as to not incur a big size and performance hit.

                How do yout think about the introduction of additional class/function template parameters?

                Chris KawaC 1 Reply Last reply
                0
                • E elfring

                  A thread safe access to QObject?

                  A few programming interfaces are thread-safe at the moment.

                  I don't see how that could be buit-in in a generic enough way as to not incur a big size and performance hit.

                  How do yout think about the introduction of additional class/function template parameters?

                  Chris KawaC Offline
                  Chris KawaC Offline
                  Chris Kawa
                  Lifetime Qt Champion
                  wrote on last edited by
                  #11

                  A few programming interfaces are thread-safe at the moment.

                  Well yeah, the ones related to threading obviously. But those are few out of thousands in the Qt library. Making everything in the library thread-safe has an inherent cost to it and it would fix one case and break thousands.

                  How do you think about the introduction of additional class/function template parameters?

                  Personally I don't need it and am not interested in it. But like I said - I'm just one user and it's an open project.
                  In any case Qt has very strict source and binary compatibility promises and this would be a fundamental change to the core classes so I doubt it will happen.

                  E 1 Reply Last reply
                  1
                  • Chris KawaC Chris Kawa

                    A few programming interfaces are thread-safe at the moment.

                    Well yeah, the ones related to threading obviously. But those are few out of thousands in the Qt library. Making everything in the library thread-safe has an inherent cost to it and it would fix one case and break thousands.

                    How do you think about the introduction of additional class/function template parameters?

                    Personally I don't need it and am not interested in it. But like I said - I'm just one user and it's an open project.
                    In any case Qt has very strict source and binary compatibility promises and this would be a fundamental change to the core classes so I doubt it will happen.

                    E Offline
                    E Offline
                    elfring
                    wrote on last edited by
                    #12

                    I'm just one user and it's an open project.

                    I am curious if other contributors would like to express more support for the mentioned software areas.

                    … so I doubt it will happen.

                    Some class and template libraries (including Qt software) were developed through the years.

                    1 Reply Last reply
                    0
                    • Christian EhrlicherC Offline
                      Christian EhrlicherC Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on last edited by
                      #13

                      You won't get an exception as 'return' value for invalid parameters in Qt in the near (and imo even far) future. Maybe Qt7 ...

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

                      E 1 Reply Last reply
                      1
                      • Christian EhrlicherC Christian Ehrlicher

                        You won't get an exception as 'return' value for invalid parameters in Qt in the near (and imo even far) future. Maybe Qt7 ...

                        E Offline
                        E Offline
                        elfring
                        wrote on last edited by
                        #14

                        Maybe Qt7 ...

                        I can eventually try also to add an implementation variant for the function “QVariantValueHelper::metaType” (on my own).

                        1 Reply Last reply
                        0

                        • Login

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