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 6771405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:25:41+00:00 2026-05-26T15:25:41+00:00

I’m using QVector to hold pointers to Objects, say FYPE*, in my program. class

  • 0

I’m using QVector to hold pointers to Objects, say FYPE*, in my program.

    class TYPE {
        // ....
    };
    const TYPE *ptrToCst;
    QVector<TYPE*> qtVct;
    // ...
    if (qtVct.contains(ptrToCst)) { // Error!!!
        // ....
    }

The compiler says QVector::contains expects TYPE* instead of const TYPE* as its parameter. A const_cast operation would solve this problem. But it doesn’t make any sense to me, since the contains method should never change what the pointer points to. And the equivalent code using STL vector works as expected.

    std::vector<TYPE*>  stlVct;
    // ...
    if (std::find(stlVct.begin(), stlVct.end(), ptrToCst)) { // Ok
        // ...
    }

What’s the reason for this difference? Did STL treat containers that hold pointers specially, so that std::find accept pointer to const object? I guess partial template specialization was involved?

  • 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-26T15:25:42+00:00Added an answer on May 26, 2026 at 3:25 pm

    This is actually a quite interesting question about const-correctness and some non-intuitive errors, but the compiler is right in rejecting the code.

    Why does is not work in Qt?

    The compiler rejects the code because it would break const-correctness of the code. I don’t know the library, but I think I can safely assume that the signature of contains is QVector<T>::contains( T const & value )[1], which in the case of T == type* means:

    QVector<type *>::contains( type * const & value )
    

    Now the problem is that you are trying to pass a variable of type type const *. The problem at this point is that the compiler does not know what contains does internally, only the promises it offers in it’s interface. contains promises not to change the pointer, but says nothing about the pointee. There is nothing in the signature inhibiting the implementation of contains from modifying the value. Consider this similar example:

    void f( int * const & p ) { // I promise not to change p
       *p = 5;                  // ... but I can modify *p
    }
    int main() {
       const int k = 10;
       int const * p = &k;
       f( p );                  // Same problem as above: if allowed, f can modify k!!!
    }
    

    Why does it allow the similar call to std::find then?

    The difference with std::find is that find is a template where the different argument type have a very loose coupling. The standard library does not perform type checking on the interface, but rather on the implementation of the template. If the argument is not of the proper type, it will be picked up while instantiating the template.

    What this means is that the implementation will be something similar to:

    template <typename Iterator, typename T>
    Iterator find( Iterator start, Iterator end, T const & value ) {
       while ( start != end && *start != value )
          ++start;
       return start;
    }
    

    The type of the iterators and the value are completely unrelated, there is no constraint there other than those imposed by the code inside the template. That means that the call will match the arguments as Iterator == std::vector<type*>::iterator and T = type const *, and the signature will match. Because internally value is only used to compare with *start, and the comparison of type * and type const * const is valid (it will convert the former to the later and then compare [2]) the code compiles perfectly.

    If the template had the extra restriction that the type of the second argument was Iterator::value_type const & (this could be implemented with SFINAE) then find would fail to compile with the same error.

    [1] Note the choice of ordering in the declaration: type const *, rather than const type *. They are exactly the same for the compiler (and the experienced eye), but by always adding the const to the right it makes it trivial to identify what is being defined as const, and to identify that const T & with T == type * in the argument of contains is not const type *&.

    [2] In the same way that you cannot bind a type * const & to a type const *, you cannot do the equivalent with pointers, type * const * cannot be converted from type const *, but if const is added to both the pointer and the pointee types, then the conversion is fine as it guarantees not to break const correctness. That conversion (which is safe) is performed in the comparison of type * == type const * const, where the left hand side gets two extra const: type const * const. If it is not clear why this does not break const-correctness, drop a comment and I will provide some code here.

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

Sidebar

Related Questions

We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm making a simple page using Google Maps API 3. My first. One marker
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload

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.