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

  • SEARCH
  • Home
  • 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 6073599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T10:17:18+00:00 2026-05-23T10:17:18+00:00

A QList<T *> can’t easily be const-correct. Consider the function void f(QList<T *> list)

  • 0

A QList<T *> can’t easily be const-correct. Consider the function

void f(QList<T *> list)
{
    list[0]->constFunction();
}

I can change f to

void f(QList<const T *> list)

but then I can’t do

f(QList<T *>());    //Compile error

anymore, since the compiler can’t implicitely cast QList<T *> to QList<const T *>. However, I can explicitely reinterpret-cast the QList as follows:

template <typename T> inline QList<const T *> &constList(const QList<T *> &list)
{
    return (QList<const T *> &)list;
}

This enables me to use the constList template function to cast any QList<T *> into a QList<const T *>, as in

f(constList(QList<T *>()));

and it seems to work fine, but is it actually safe to do this?

  • 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-05-23T10:17:19+00:00Added an answer on May 23, 2026 at 10:17 am

    The casting function you’re considering, …

    template< class T >
    inline QList<T const*>& constList( QList<T*> const& list )
    {
        return reinterpret_cast< QList<T const*>& >(
            const_cast< QList<T*>& >( list )
            );
    }
    

    … may be practical (probably QList does not change its object representation depending on the const-ness of the element type), but it can break const correctness.

    First, because casting away the const-ness of the list itself is not const correct: it allows you to change an originally const list.

    But even if that formal argument const is removed, like …

    template< class T >
    inline QList<T const*>& constList( QList<T*>& list )
    {
        return reinterpret_cast< QList<T const*>& >(
            list
            );
    }
    

    … there is still a const correctness problem.

    The reason is that the list constitutes an additional level of indirection, and with your function is not itself const. Thus, after using your function to get a reference to the list with alleged pointer-to-const elements, you can store in that list a pointer to something that is really const. And then you can use the original list to modify that really const item, bang.

    It’s the same reason that there is no implicit conversion from T** to T const**.

    What you can do without such problems is, with an already const list of pointers to objects, make those pointed to objects const:

    template< class T >
    inline QList<T const*> const& constList( QList<T*> const& list )
    {
        return reinterpret_cast< QList<T const*> const& >(
            list
            );
    }
    

    Formally there’s still the reinterpret_cast as a potential problem, but anyone specializating the representation of QList on constness of elements would presumably deserve whatever they got. 🙂

    Cheers & hth.,

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have q method that shown at below: void MainWindow::slotResults( const QList<QSqlRecord>& records )
I'm trying to append items to a QList at runtime, but I'm running on
As i can see only list method in query class in hibernate.How to get
I'm using Qt 4.5 and im working with a QList<QStringList> which is a list
I want to know, How QVariant can internally stores, int, QMap, QList,... I mean
just a brief QList intro, QList is similar to QVector and STL vector but
I'm writing a Qt GUI Application, but there's a strange error i can't figure
I am using compare function to sort QList and MyClass has n different attribute(s)
I'm trying to free memory after using QList, but it doesn't seem to work
I am trying to get data from LivEx table as list, and then save

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.