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. Increasing usage for C++ new operators based on data model indexes?
QtWS25 Last Chance

Increasing usage for C++ new operators based on data model indexes?

Scheduled Pinned Locked Moved Unsolved General and Desktop
data modelscreateindexallocationnew operatorssoftware design
116 Posts 6 Posters 51.3k 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.
  • K kshegunov
    14 Oct 2018, 23:14

    @elfring said in Increasing usage for C++ new operators based on data model indexes?:

    A C++ new operator can provide a known pointer data type, can't it?

    If you mean that pointer to be void *, then yes, formally. However void * (a.k.a. the "opaque pointer") is even worse, as you don't get any type safety with it. QVariant does the same thing, but more cleanly. It keeps taps on what data was put into it and even provides conversions through the meta type system.

    Some data structures in the C++ standard template library get such a parameter passed.

    Are you willing to try and mix templates and QObjects? If so, call me in, I want to watch the fireworks.

    It is just a function which should return a valid pointer for a storage location.

    The model does not do storage! And that's what @VRonin has mentioned couple of times already. The model is your "map" to the data - what is located where, no more - no less.

    Under which circumstances would C++ programmers call it like “new”?

    Look, I get that you like new, but the heap is approximately 10 (and sometimes more) times slower than the stack. There's no really conceivable reason for anyone to create the model index in the heap ... it just does not make any sense.

    Say you're working with points in 3d space - you have 3 coordinates that define the point (i.e. your structure/class has 3 members representing the coordinates), would you go around creating those objects representing points in the heap?

    E Offline
    E Offline
    elfring
    wrote on 15 Oct 2018, 07:44 last edited by
    #14

    If you mean that pointer to be void *, …

    No! - Must a C++ new operator provide a non-void pointer data type?

    The model does not do storage!

    It provides the generic programming interface for the desired data sources.
    Derived classes will contain member variables which will manage storage in expected ways, won't they?

    Look, I get that you like new, …

    C++ programmers are using this operator for various resource allocations.
    I propose to increase the usage of the construct “placement new” also together with customised data models.

    K 1 Reply Last reply 15 Oct 2018, 08:04
    0
    • K kshegunov
      14 Oct 2018, 23:14

      @elfring said in Increasing usage for C++ new operators based on data model indexes?:

      A C++ new operator can provide a known pointer data type, can't it?

      If you mean that pointer to be void *, then yes, formally. However void * (a.k.a. the "opaque pointer") is even worse, as you don't get any type safety with it. QVariant does the same thing, but more cleanly. It keeps taps on what data was put into it and even provides conversions through the meta type system.

      Some data structures in the C++ standard template library get such a parameter passed.

      Are you willing to try and mix templates and QObjects? If so, call me in, I want to watch the fireworks.

      It is just a function which should return a valid pointer for a storage location.

      The model does not do storage! And that's what @VRonin has mentioned couple of times already. The model is your "map" to the data - what is located where, no more - no less.

      Under which circumstances would C++ programmers call it like “new”?

      Look, I get that you like new, but the heap is approximately 10 (and sometimes more) times slower than the stack. There's no really conceivable reason for anyone to create the model index in the heap ... it just does not make any sense.

      Say you're working with points in 3d space - you have 3 coordinates that define the point (i.e. your structure/class has 3 members representing the coordinates), would you go around creating those objects representing points in the heap?

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 15 Oct 2018, 07:55 last edited by
      #15

      @kshegunov said in Increasing usage for C++ new operators based on data model indexes?:

      Look, I get that you like new, but the heap is approximately 10 (and sometimes more) times slower than the stack.

      Just a question out of curiosity, because I remeber by instructor also telling me c++ heap allocation is slower than stack and slower than heap allocation in other languages.

      Is there a different method in c++ to allocation memory instead of the standard new?


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      0
      • E elfring
        15 Oct 2018, 07:44

        If you mean that pointer to be void *, …

        No! - Must a C++ new operator provide a non-void pointer data type?

        The model does not do storage!

        It provides the generic programming interface for the desired data sources.
        Derived classes will contain member variables which will manage storage in expected ways, won't they?

        Look, I get that you like new, …

        C++ programmers are using this operator for various resource allocations.
        I propose to increase the usage of the construct “placement new” also together with customised data models.

        K Offline
        K Offline
        kshegunov
        Moderators
        wrote on 15 Oct 2018, 08:04 last edited by
        #16

        @elfring said in Increasing usage for C++ new operators based on data model indexes?:

        Must a C++ new operator provide a non-void pointer data type?

        It can provide whatever type you want it to, however I still don't see how you're going to have a generic type (i.e. QAbstractItemModel) that deals with concrete types it does know nothing about ...

        It provides the generic programming interface for the desired data sources.

        Indeed.

        Derived classes will contain member variables which will manage storage in expected ways, won't they?

        They may or may not. It's up to the user code to decide where and how the storage will be done. There may be no storage at all and the data to be fetched on the fly from someplace.

        C++ programmers are using this operator for various resource allocations.

        Many C++ programmers overuse this operator, especially those with limited experience.

        I propose to increase the usage of the construct “placement new” also together with customised data models.

        https://www.youtube.com/watch?v=iUQCFI02zZA

        @J.Hilk said in Increasing usage for C++ new operators based on data model indexes?:

        Just a question out of curiosity, because I remeber by instructor also telling me c++ heap allocation is slower than stack

        It is. It's a call to the heap manager in the OS. The heap manager has to find a free place for your object before it can return you an address ...
        A stack allocation is nothing on the other hand - you (rather the compiler) move the stack pointer to the appropriate offset from the stack base pointer and voila - you have memory. That also is the reason that you must know the size of the allocated object at compile time.

        and slower than heap allocation in other languages.

        Nope!

        Is there a different method in c++ to allocation memory instead of the standard new?

        You have *alloc from the C runtime, the placement new if you're crazy enough to build your own heap manager (or for some very specific similar purposes), and of course you have my buddy - the stack.

        Read and abide by the Qt Code of Conduct

        J E 2 Replies Last reply 15 Oct 2018, 08:12
        5
        • K kshegunov
          15 Oct 2018, 08:04

          @elfring said in Increasing usage for C++ new operators based on data model indexes?:

          Must a C++ new operator provide a non-void pointer data type?

          It can provide whatever type you want it to, however I still don't see how you're going to have a generic type (i.e. QAbstractItemModel) that deals with concrete types it does know nothing about ...

          It provides the generic programming interface for the desired data sources.

          Indeed.

          Derived classes will contain member variables which will manage storage in expected ways, won't they?

          They may or may not. It's up to the user code to decide where and how the storage will be done. There may be no storage at all and the data to be fetched on the fly from someplace.

          C++ programmers are using this operator for various resource allocations.

          Many C++ programmers overuse this operator, especially those with limited experience.

          I propose to increase the usage of the construct “placement new” also together with customised data models.

          https://www.youtube.com/watch?v=iUQCFI02zZA

          @J.Hilk said in Increasing usage for C++ new operators based on data model indexes?:

          Just a question out of curiosity, because I remeber by instructor also telling me c++ heap allocation is slower than stack

          It is. It's a call to the heap manager in the OS. The heap manager has to find a free place for your object before it can return you an address ...
          A stack allocation is nothing on the other hand - you (rather the compiler) move the stack pointer to the appropriate offset from the stack base pointer and voila - you have memory. That also is the reason that you must know the size of the allocated object at compile time.

          and slower than heap allocation in other languages.

          Nope!

          Is there a different method in c++ to allocation memory instead of the standard new?

          You have *alloc from the C runtime, the placement new if you're crazy enough to build your own heap manager (or for some very specific similar purposes), and of course you have my buddy - the stack.

          J Offline
          J Offline
          J.Hilk
          Moderators
          wrote on 15 Oct 2018, 08:12 last edited by
          #17

          @kshegunov thanks for the info.

          IIRC boost also has special allocation methods for faster instantiation of objects. But I'm not sure, never used that libary much.


          Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


          Q: What's that?
          A: It's blue light.
          Q: What does it do?
          A: It turns blue.

          1 Reply Last reply
          0
          • E elfring
            12 Oct 2018, 20:22

            Why would you want to?

            The concrete target data types can vary while model indexes are resolved to QVariant objects as the default data.
            A C++ new operator can provide a known pointer data type, can't it?

            what is a "model storage allocator"?

            Some data structures in the C++ standard template library get such a parameter passed.

            V Offline
            V Offline
            VRonin
            wrote on 15 Oct 2018, 08:59 last edited by
            #18

            @elfring said in Increasing usage for C++ new operators based on data model indexes?:

            The concrete target data types can vary while model indexes are resolved to QVariant objects as the default data.
            A C++ new operator can provide a known pointer data type, can't it?

            Isn't dynamic_cast what you want?

            @kshegunov said in Increasing usage for C++ new operators based on data model indexes?:

            You have *alloc from the C runtime

            Note for passers-by: If you use those to allocate complex types the constructor may/will not be called

            "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
            ~Napoleon Bonaparte

            On a crusade to banish setIndexWidget() from the holy land of Qt

            E 1 Reply Last reply 15 Oct 2018, 10:52
            4
            • K kshegunov
              15 Oct 2018, 08:04

              @elfring said in Increasing usage for C++ new operators based on data model indexes?:

              Must a C++ new operator provide a non-void pointer data type?

              It can provide whatever type you want it to, however I still don't see how you're going to have a generic type (i.e. QAbstractItemModel) that deals with concrete types it does know nothing about ...

              It provides the generic programming interface for the desired data sources.

              Indeed.

              Derived classes will contain member variables which will manage storage in expected ways, won't they?

              They may or may not. It's up to the user code to decide where and how the storage will be done. There may be no storage at all and the data to be fetched on the fly from someplace.

              C++ programmers are using this operator for various resource allocations.

              Many C++ programmers overuse this operator, especially those with limited experience.

              I propose to increase the usage of the construct “placement new” also together with customised data models.

              https://www.youtube.com/watch?v=iUQCFI02zZA

              @J.Hilk said in Increasing usage for C++ new operators based on data model indexes?:

              Just a question out of curiosity, because I remeber by instructor also telling me c++ heap allocation is slower than stack

              It is. It's a call to the heap manager in the OS. The heap manager has to find a free place for your object before it can return you an address ...
              A stack allocation is nothing on the other hand - you (rather the compiler) move the stack pointer to the appropriate offset from the stack base pointer and voila - you have memory. That also is the reason that you must know the size of the allocated object at compile time.

              and slower than heap allocation in other languages.

              Nope!

              Is there a different method in c++ to allocation memory instead of the standard new?

              You have *alloc from the C runtime, the placement new if you're crazy enough to build your own heap manager (or for some very specific similar purposes), and of course you have my buddy - the stack.

              E Offline
              E Offline
              elfring
              wrote on 15 Oct 2018, 10:50 last edited by
              #19

              …, however I still don't see how you're going to have a generic type (i.e. QAbstractItemModel) that deals with concrete types it does know nothing about ...

              Various data structures are accessible over pointers (or customised indexes?).

              Does the class “QVariant” provide also a programming interface for pointer data types?

              …, the placement new if you're crazy enough to build your own heap manager (or for some very specific similar purposes), …

              I am trying to increase the software development attention for the latter.

              V 1 Reply Last reply 15 Oct 2018, 10:54
              0
              • V VRonin
                15 Oct 2018, 08:59

                @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                The concrete target data types can vary while model indexes are resolved to QVariant objects as the default data.
                A C++ new operator can provide a known pointer data type, can't it?

                Isn't dynamic_cast what you want?

                @kshegunov said in Increasing usage for C++ new operators based on data model indexes?:

                You have *alloc from the C runtime

                Note for passers-by: If you use those to allocate complex types the constructor may/will not be called

                E Offline
                E Offline
                elfring
                wrote on 15 Oct 2018, 10:52 last edited by
                #20

                Isn't dynamic_cast what you want?

                Yes.

                I prefer to avoid null pointers which can eventually be returned by such a cast operation.

                1 Reply Last reply
                0
                • E elfring
                  15 Oct 2018, 10:50

                  …, however I still don't see how you're going to have a generic type (i.e. QAbstractItemModel) that deals with concrete types it does know nothing about ...

                  Various data structures are accessible over pointers (or customised indexes?).

                  Does the class “QVariant” provide also a programming interface for pointer data types?

                  …, the placement new if you're crazy enough to build your own heap manager (or for some very specific similar purposes), …

                  I am trying to increase the software development attention for the latter.

                  V Offline
                  V Offline
                  VRonin
                  wrote on 15 Oct 2018, 10:54 last edited by VRonin
                  #21

                  @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                  Does the class “QVariant” provide also a programming interface for pointer data types?

                  Yes using QVariant::fromValue or via the constructor if the type is a QObject subclass.
                  hic sunt leones though as you need to make clear in your logic who owns the items pointed. In general a QVariant containing a owning pointer is a ticking time bomb toward memory leak and/or double deletion.

                  I prefer to avoid null pointers which can eventually be returned by such a cast operation.

                  If you are certain of the downcast type you can even use static_cast and forget about null pointers

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  E 1 Reply Last reply 15 Oct 2018, 11:31
                  1
                  • V VRonin
                    15 Oct 2018, 10:54

                    @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                    Does the class “QVariant” provide also a programming interface for pointer data types?

                    Yes using QVariant::fromValue or via the constructor if the type is a QObject subclass.
                    hic sunt leones though as you need to make clear in your logic who owns the items pointed. In general a QVariant containing a owning pointer is a ticking time bomb toward memory leak and/or double deletion.

                    I prefer to avoid null pointers which can eventually be returned by such a cast operation.

                    If you are certain of the downcast type you can even use static_cast and forget about null pointers

                    E Offline
                    E Offline
                    elfring
                    wrote on 15 Oct 2018, 11:31 last edited by
                    #22

                    In general a QVariant containing a owning pointer is a ticking time bomb toward memory leak and/or double deletion.

                    • The safe handling of object lifetimes is a general software development challenge.
                    • But this class is the only way to get data from a model so far, isn't it?

                    If you are certain of the downcast type you can even use static_cast and forget about null pointers

                    Can it be more convenient to let a C++ new operator (which can work also with extra allocation parameters directly) perform the desired data type conversion?

                    V 1 Reply Last reply 15 Oct 2018, 11:53
                    0
                    • E elfring
                      15 Oct 2018, 11:31

                      In general a QVariant containing a owning pointer is a ticking time bomb toward memory leak and/or double deletion.

                      • The safe handling of object lifetimes is a general software development challenge.
                      • But this class is the only way to get data from a model so far, isn't it?

                      If you are certain of the downcast type you can even use static_cast and forget about null pointers

                      Can it be more convenient to let a C++ new operator (which can work also with extra allocation parameters directly) perform the desired data type conversion?

                      V Offline
                      V Offline
                      VRonin
                      wrote on 15 Oct 2018, 11:53 last edited by
                      #23

                      @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                      The safe handling of object lifetimes is a general software development challenge.
                      But this class is the only way to get data from a model so far, isn't it?

                      Yes, what I'm saying is that if you want to store a pointer in a QVariant you probably want a non-owning pointer. Keep the owning pointer somewhere else.

                      Can it be more convenient to let a C++ new operator (which can work also with extra allocation parameters directly) perform the desired data type conversion?

                      No, new allocates memory. static_cast just tell the compiler to treat a piece of memory as it was a different type.

                      But this class is the only way to get data from a model so far, isn't it?

                      And here we come back to the fact that having your objects implicitly shared stored in a QVariant directly is much more practical and has a negligible impact on performance

                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                      ~Napoleon Bonaparte

                      On a crusade to banish setIndexWidget() from the holy land of Qt

                      E 1 Reply Last reply 15 Oct 2018, 13:01
                      1
                      • V VRonin
                        15 Oct 2018, 11:53

                        @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                        The safe handling of object lifetimes is a general software development challenge.
                        But this class is the only way to get data from a model so far, isn't it?

                        Yes, what I'm saying is that if you want to store a pointer in a QVariant you probably want a non-owning pointer. Keep the owning pointer somewhere else.

                        Can it be more convenient to let a C++ new operator (which can work also with extra allocation parameters directly) perform the desired data type conversion?

                        No, new allocates memory. static_cast just tell the compiler to treat a piece of memory as it was a different type.

                        But this class is the only way to get data from a model so far, isn't it?

                        And here we come back to the fact that having your objects implicitly shared stored in a QVariant directly is much more practical and has a negligible impact on performance

                        E Offline
                        E Offline
                        elfring
                        wrote on 15 Oct 2018, 13:01 last edited by
                        #24

                        new allocates memory.

                        Only if you do not use the construct “placement new”.

                        And here we come back to the fact that having your objects implicitly shared stored in a QVariant directly is much more practical

                        I guess that the run time consequences are more interesting then for the distinction if only pointers are transferred or complete “value objects” are copied.

                        and has a negligible impact on performance

                        There are software applications where more data transfers might not have a directly noticeable effect. But I guess that some C++ programmers have got a strong focus on software efficiency.

                        K V 2 Replies Last reply 15 Oct 2018, 13:09
                        0
                        • E elfring
                          15 Oct 2018, 13:01

                          new allocates memory.

                          Only if you do not use the construct “placement new”.

                          And here we come back to the fact that having your objects implicitly shared stored in a QVariant directly is much more practical

                          I guess that the run time consequences are more interesting then for the distinction if only pointers are transferred or complete “value objects” are copied.

                          and has a negligible impact on performance

                          There are software applications where more data transfers might not have a directly noticeable effect. But I guess that some C++ programmers have got a strong focus on software efficiency.

                          K Offline
                          K Offline
                          kshegunov
                          Moderators
                          wrote on 15 Oct 2018, 13:09 last edited by
                          #25

                          This is getting pretty ridiculous ...

                          @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                          Only if you do not use the construct “placement new”.

                          Yeah, which I have a creeping suspicion you haven't. The so called placement new is nothing more than going around calling constructors on a preallocated memory block. So where is this preallocated memory going to come from? It's going to materialize from the ether?

                          If you're intent on pushing this make-you-own-heap-for-models, I advise to create a proof-of-concept first, then we can have something to base a discussion on, otherwise - thanks but no thanks, I'm out of this conversation.

                          Read and abide by the Qt Code of Conduct

                          E 1 Reply Last reply 15 Oct 2018, 13:22
                          0
                          • K kshegunov
                            15 Oct 2018, 13:09

                            This is getting pretty ridiculous ...

                            @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                            Only if you do not use the construct “placement new”.

                            Yeah, which I have a creeping suspicion you haven't. The so called placement new is nothing more than going around calling constructors on a preallocated memory block. So where is this preallocated memory going to come from? It's going to materialize from the ether?

                            If you're intent on pushing this make-you-own-heap-for-models, I advise to create a proof-of-concept first, then we can have something to base a discussion on, otherwise - thanks but no thanks, I'm out of this conversation.

                            E Offline
                            E Offline
                            elfring
                            wrote on 15 Oct 2018, 13:22 last edited by
                            #26

                            The so called placement new is nothing more than going around calling constructors on a preallocated memory block.

                            I agree on this aspect.

                            But can you pass a “model index” as another allocation parameter (for existing data) to this C++ operator?

                            1 Reply Last reply
                            0
                            • E elfring
                              15 Oct 2018, 13:01

                              new allocates memory.

                              Only if you do not use the construct “placement new”.

                              And here we come back to the fact that having your objects implicitly shared stored in a QVariant directly is much more practical

                              I guess that the run time consequences are more interesting then for the distinction if only pointers are transferred or complete “value objects” are copied.

                              and has a negligible impact on performance

                              There are software applications where more data transfers might not have a directly noticeable effect. But I guess that some C++ programmers have got a strong focus on software efficiency.

                              V Offline
                              V Offline
                              VRonin
                              wrote on 15 Oct 2018, 13:24 last edited by VRonin
                              #27

                              @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                              Only if you do not use the construct “placement new”.

                              Is your plan to do exactly what dynamic_cast does but using a new operator? I mean it's possible but I don't see why you'd want to do it. How would you implement it?

                              guess that the run time consequences are more interesting then for the distinction if only pointers are transferred or complete “value objects” are copied.
                              There are software applications where more data transfers might not have a directly noticeable effect. But I guess that some C++ programmers have got a strong focus on software efficiency.

                              @VRonin said in Increasing usage for C++ new operators based on data model indexes?:

                              having your objects implicitly shared

                              With implicitly shared objects, the distinction from a pointer to a complete value object when copying is just an operator++ on an int. 1 atomic instruction. 100000 times more efficient than a dynamic_cast.
                              If you want to go into memory difference it's 32bits more. If 32 bits break your design then it's the design itself that comes into question

                              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                              ~Napoleon Bonaparte

                              On a crusade to banish setIndexWidget() from the holy land of Qt

                              E 1 Reply Last reply 15 Oct 2018, 13:32
                              0
                              • V VRonin
                                15 Oct 2018, 13:24

                                @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                                Only if you do not use the construct “placement new”.

                                Is your plan to do exactly what dynamic_cast does but using a new operator? I mean it's possible but I don't see why you'd want to do it. How would you implement it?

                                guess that the run time consequences are more interesting then for the distinction if only pointers are transferred or complete “value objects” are copied.
                                There are software applications where more data transfers might not have a directly noticeable effect. But I guess that some C++ programmers have got a strong focus on software efficiency.

                                @VRonin said in Increasing usage for C++ new operators based on data model indexes?:

                                having your objects implicitly shared

                                With implicitly shared objects, the distinction from a pointer to a complete value object when copying is just an operator++ on an int. 1 atomic instruction. 100000 times more efficient than a dynamic_cast.
                                If you want to go into memory difference it's 32bits more. If 32 bits break your design then it's the design itself that comes into question

                                E Offline
                                E Offline
                                elfring
                                wrote on 15 Oct 2018, 13:32 last edited by
                                #28

                                Is your plan to do exactly what dynamic_cast does but using a new operator?

                                Maybe.

                                But these C++ operations provide different functionality, don't they?

                                With implicitly shared objects, the distinction from a pointer to a complete value object when copying is just an operator++ on an int.

                                Will it become nicer to avoid (or reduce) even the influence of object reference counting?

                                V 1 Reply Last reply 15 Oct 2018, 14:39
                                0
                                • E elfring
                                  15 Oct 2018, 13:32

                                  Is your plan to do exactly what dynamic_cast does but using a new operator?

                                  Maybe.

                                  But these C++ operations provide different functionality, don't they?

                                  With implicitly shared objects, the distinction from a pointer to a complete value object when copying is just an operator++ on an int.

                                  Will it become nicer to avoid (or reduce) even the influence of object reference counting?

                                  V Offline
                                  V Offline
                                  VRonin
                                  wrote on 15 Oct 2018, 14:39 last edited by
                                  #29

                                  @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                                  Will it become nicer to avoid (or reduce) even the influence of object reference counting?

                                  C++ is moving in the opposite direction actually. If you try to suggest replacing std::shared_ptr (which is a pointer + a reference counter) with raw pointers on Stack Overflow you'd better bring a helmet and kevlar vest because you are going to get shot.
                                  The ISO C++ style guide goes one step further and even discourages the use of owning raw pointer and suggest ownership encapsulation (an implicitly shared object behaves as a smart pointer). If you don't like it, good luck convincing Herb Sutter and Bjarne Stroustrup himself.

                                  But these C++ operations provide different functionality, don't they?

                                  Yes but so far you did not highlight any need for custom allocation, your problems all seem to come from casting.

                                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                  ~Napoleon Bonaparte

                                  On a crusade to banish setIndexWidget() from the holy land of Qt

                                  E 1 Reply Last reply 15 Oct 2018, 16:08
                                  2
                                  • V VRonin
                                    15 Oct 2018, 14:39

                                    @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                                    Will it become nicer to avoid (or reduce) even the influence of object reference counting?

                                    C++ is moving in the opposite direction actually. If you try to suggest replacing std::shared_ptr (which is a pointer + a reference counter) with raw pointers on Stack Overflow you'd better bring a helmet and kevlar vest because you are going to get shot.
                                    The ISO C++ style guide goes one step further and even discourages the use of owning raw pointer and suggest ownership encapsulation (an implicitly shared object behaves as a smart pointer). If you don't like it, good luck convincing Herb Sutter and Bjarne Stroustrup himself.

                                    But these C++ operations provide different functionality, don't they?

                                    Yes but so far you did not highlight any need for custom allocation, your problems all seem to come from casting.

                                    E Offline
                                    E Offline
                                    elfring
                                    wrote on 15 Oct 2018, 16:08 last edited by
                                    #30

                                    but so far you did not highlight any need for custom allocation,

                                    I suggest to reconsider the software situation once more.

                                    A pair of row and column values (data model index, address or coordinate) can be passed as a parameter to a C++ new operator.
                                    You can choose then if you need to work with fresh objects (using dynamic memory allocation) or would like to reuse existing data.

                                    Is the usage of the construct “placement new” a kind of customised operation?

                                    your problems all seem to come from casting.

                                    I hope that specific software development challenges can be adjusted when the new operator call takes care of casting to the desired target (pointer) data type already.

                                    V 1 Reply Last reply 15 Oct 2018, 17:09
                                    0
                                    • E elfring
                                      15 Oct 2018, 16:08

                                      but so far you did not highlight any need for custom allocation,

                                      I suggest to reconsider the software situation once more.

                                      A pair of row and column values (data model index, address or coordinate) can be passed as a parameter to a C++ new operator.
                                      You can choose then if you need to work with fresh objects (using dynamic memory allocation) or would like to reuse existing data.

                                      Is the usage of the construct “placement new” a kind of customised operation?

                                      your problems all seem to come from casting.

                                      I hope that specific software development challenges can be adjusted when the new operator call takes care of casting to the desired target (pointer) data type already.

                                      V Offline
                                      V Offline
                                      VRonin
                                      wrote on 15 Oct 2018, 17:09 last edited by
                                      #31

                                      @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                                      A pair of row and column values (data model index, address or coordinate) can be passed as a parameter to a C++ new operator.
                                      You can choose then if you need to work with fresh objects (using dynamic memory allocation) or would like to reuse existing data.
                                      Is the usage of the construct “placement new” a kind of customised operation?

                                      Once again, the model implementation might or might not allocate data. This should not be a problem of the API user.

                                      Could you please provide a concrete example of what you are suggesting?
                                      Otherwise we are just discussing of theory and nobody is gaining any value out of it.
                                      I will not continue the discussion until you provide at least a code snippet

                                      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                                      ~Napoleon Bonaparte

                                      On a crusade to banish setIndexWidget() from the holy land of Qt

                                      E 2 Replies Last reply 15 Oct 2018, 17:27
                                      3
                                      • V VRonin
                                        15 Oct 2018, 17:09

                                        @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                                        A pair of row and column values (data model index, address or coordinate) can be passed as a parameter to a C++ new operator.
                                        You can choose then if you need to work with fresh objects (using dynamic memory allocation) or would like to reuse existing data.
                                        Is the usage of the construct “placement new” a kind of customised operation?

                                        Once again, the model implementation might or might not allocate data. This should not be a problem of the API user.

                                        Could you please provide a concrete example of what you are suggesting?
                                        Otherwise we are just discussing of theory and nobody is gaining any value out of it.
                                        I will not continue the discussion until you provide at least a code snippet

                                        E Offline
                                        E Offline
                                        elfring
                                        wrote on 15 Oct 2018, 17:27 last edited by
                                        #32

                                        Could you please provide a concrete example of what you are suggesting?

                                        Does the software situation become really more challenging for passing a few extra parameters to a (member) function when it is “accidentally” (or intentionally) called “new”?

                                        1 Reply Last reply
                                        0
                                        • V VRonin
                                          15 Oct 2018, 17:09

                                          @elfring said in Increasing usage for C++ new operators based on data model indexes?:

                                          A pair of row and column values (data model index, address or coordinate) can be passed as a parameter to a C++ new operator.
                                          You can choose then if you need to work with fresh objects (using dynamic memory allocation) or would like to reuse existing data.
                                          Is the usage of the construct “placement new” a kind of customised operation?

                                          Once again, the model implementation might or might not allocate data. This should not be a problem of the API user.

                                          Could you please provide a concrete example of what you are suggesting?
                                          Otherwise we are just discussing of theory and nobody is gaining any value out of it.
                                          I will not continue the discussion until you provide at least a code snippet

                                          E Offline
                                          E Offline
                                          elfring
                                          wrote on 15 Oct 2018, 19:55 last edited by
                                          #33

                                          … concrete example of what you are suggesting?

                                          • https://isocpp.org/wiki/faq/dtors#placement-new
                                          • https://en.wikipedia.org/wiki/Placement_syntax
                                          • https://stackoverflow.com/questions/222557/what-uses-are-there-for-placement-new
                                          • https://www.geeksforgeeks.org/placement-new-operator-cpp/
                                          • http://blog.aaronballman.com/2011/08/the-placement-new-operator/
                                          • https://thispointer.com/whats-placement-new-operator-and-why-do-we-need-it/
                                          • https://eli.thegreenplace.net/2011/02/17/the-many-faces-of-operator-new-in-c
                                          • https://archive.org/details/TICPP2ndEdVolOne
                                          1 Reply Last reply
                                          0

                                          23/116

                                          15 Oct 2018, 11:53

                                          93 unread
                                          • Login

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