Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8116395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T03:49:17+00:00 2026-06-06T03:49:17+00:00

I dug up an old Grid class, which is just a simple 2-D container

  • 0

I dug up an old Grid class, which is just a simple 2-D container templated with a type. To make one you would do this:

Grid<SomeType> myGrid (QSize (width, height));

I tried to make it “Qt-ish”…for instance it does size operations in terms of QSize, and you index into it with myGrid[QPoint (x, y)]. It can take boolean masks and do operations on elements whose mask bit was set. There’s also a specialization where if your elements are QColor it can generate a QImage for you.

But one major Qt idiom I adopted was that it did implicit sharing under the hood. This turned out to be very useful in the QColor-based grids for the Thinker-Qt-based program I had.

However :-/ I also happened to have some cases where I’d written the likes of:

Grid< auto_ptr<SomeType> > myAutoPtrGrid (QSize (width, height));

When I moved up from auto_ptr to C++11’s unique_ptr, the compiler rightfully complained. Implicit sharing requires the ability to make an identical copy if needed…and auto_ptr had swept this bug under the rug by conflating copying with transfer-of-ownership. Non-copyable types and implicit sharing simply do not mix, and unique_ptr is kind enough to tell us.

(Note: It so happened that I hadn’t noticed the problem in practice, because the use cases for the auto_ptr were passing grids by reference…never by value. Still, this was bad code…and the proactive nature of C++11 is pointing out the potential problem before it happens.)

Ok, so…how might I design a generic container that can flip implicit sharing on and off? I really did want many of the Grid features when I was using the auto_ptr and it’s great if copying is disabled for non-copyable types…that catches errors! But having the implicit sharing work is nice as a default, when the type happens to be copyable.

Some ideas:

  • I could make separate types (NonCopyableGrid, CopyableGrid)…or (UniqueGrid, Grid) depending on your tastes…
  • I could pass a flag into the Grid constructor
  • I could use static factory methods (Grid::newNonCopyable, Grid::newCopyable) but which would call the relevant constructor under the hood…maybe more descriptive
  • If possible, I might “detect” copyability on the contained type, and then either leverage a QSharedDataPointer in the implementation or not, depending?

Any good reasons to pick one of these methods over the others, or have people adopted something altogether better for this kind of situation?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T03:49:18+00:00Added an answer on June 6, 2026 at 3:49 am

    If you were going to do it in a single container, I think the easiest way would be to use std::is_copy_constructable to choose whether your data struct inherited from QSharedData, and to replace QSharedDataPointer with std::unique_ptr (QScopedPointer doesn’t support move semantics)

    This is only a rough example of what I’m thinking as I don’t have Qt and C++11 available together:

    template<class T>
    class Grid
    {       
        struct EmptyStruct
        {
        };
    
        typedef typename std::conditional<
            std::is_copy_constructible<T>::value,  
            QSharedData,  
            EmptyStruct
        >::type GridDataBase;
    
        struct GridData : public GridDataBase
        {
            // data goes here
        };
    
        typedef typename std::conditional<
            std::is_copy_constructible<T>::value, 
            QSharedDataPointer<GridData>, 
            std::unique_ptr<GridData>
        >::type GridDataPointer;
    
    public:
        Grid() : data_(new GridData) {}
    
    private:
        GridDataPointer data_;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm just putting the finishing touches to my Android app. Unfortunately, I dug straight
I am fairly new to C#, and have just dug in. I got my
We tried last night to build some code which would create a new public
I am battling with this issue for a long time, dug around Google and
I'm sorry if this is a Uhh RTFM type of question, but I've checked
I've dug the manual and tried extensivelly to drop the extra function(){...} call after
All the questions I have dug through in the boards aren't really answering a
I am new to Unit Testing and think I might have dug myself into
After years in embedded programming, I have to develop a Windows app. I dug
I have been trying to get TTTabItem to work with images. And I dug

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.