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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:45:54+00:00 2026-06-17T06:45:54+00:00

the question is pretty simple, is it in general safe a static cast (or

  • 0

the question is pretty simple, is it in general safe a static cast (or some other cast) from

std::vector< Foo >

to

std::vector< const Foo >

binary-wise, i don’t see why the native types would differ, after all, the const is a language constraint that should not affect the size of the element, or so i think

can i do

std::vector< const Foo >& someFunc()
{
  std::vector< Foo >& ref = ...
  return *reinterpret_cast<std::vector< const Foo >*>(& ref);
}

and not worry that this will sink someone’s boat? or is this unsafe in general?

  • 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-17T06:45:55+00:00Added an answer on June 17, 2026 at 6:45 am

    Ignoring that you said std::vector for the moment and pretending you had some other less well defined vector implementation. Your code would be technically unsafe not because T and T const are quite different but because the C++ language permits vector<T> and vector<T const> to be specialised in ways that are quite different. Consider the following code:

    #include <iostream>
    
    template <class T>
    struct vector {
        T* start_;
        T* end_;
        T* cap_end_;
    };
    
    template <class T>
    struct vector<T const> {
        bool gotcha_;
        T* start_;
        T* end_;
        T* cap_end_;
    };
    
    struct foo { };
    
    int
    main()
    {
        std::cout
            << sizeof(vector<foo>) << '\n'
            << sizeof(vector<foo const>) << '\n'
            ;
    }
    

    Note that there are other more pernicious changes that could make your life miserable. Such as the following where the members are reordered:

    #include <iostream>
    
    template <class T>
    struct vector {
        T* start_;
        T* end_;
        T* cap_end_;
    };
    
    template <class T>
    struct vector<T const> {
        T* end_;
        T* cap_end_;
        T* start_;
    };
    
    template <class T>
    long size(vector<T> const& v)
    {
        return v.end_ - v.start_;
    }
    
    struct foo { };
    
    int
    main()
    {
        vector<foo> v;
        v.start_ = new foo[10];
        v.end_ = v.start_ + 1;
        v.cap_end_ = v.start_ + 10;
    
    
        std::cout
            << size(v) << '\n'
            << size(*reinterpret_cast<vector<foo const>*>(&v)) << '\n'
            ;
    
        return 0;
    }
    

    Wrt to std::vector, I am not familiar enough with the fine details of the standard library specification to know whether such specialisations would be conformant or not. Perhaps someone more well versed in the standard can comment.

    Note some of what I said in answer to Casting templated class to more general specialization may help explain this problem.

    To address your question about detecting specialisations there are ways to make your code unsafe by using no specialisations of the class but overloaded non-member functions and I am o not sure how you would detect that. Such as in the following:

    #include <iostream>
    
    template <class T>
    struct vector {
        T* start_;
        T* end_;
        T* cap_end_;
    };
    
    
    template <class T>
    void init(vector<T>& v, size_t sz, size_t cap)
    {
        v.start_ = new T[cap];
        v.end_ = v.start_ + sz;
        v.cap_end_ = v.start_ + cap;
    }
    
    template <class T>
    void init(vector<T const>& v, size_t sz, size_t cap)
    {
        v.end_ = new T const[cap];
        v.cap_end_ = v.end_ + sz;
        v.start_ = v.end_ + cap;
    }
    
    template <class T>
    long size(vector<T>& v)
    {
        return v.end_ - v.start_;
    }
    
    template <class T>
    long size(vector<T const>& v)
    {
        return v.cap_end_ - v.end_;
    }
    
    struct foo { };
    
    int
    main()
    {
        vector<foo const> v;
        init(v, 1, 10);
    
        std::cout
            << size(v) << '\n'
            << size(*reinterpret_cast<vector<foo>*>(&v)) << '\n'
            ;
    }
    

    Enough with the bad news. The good news is that if you want to take an existing object with a general interface and restrict or adjust what can be done with that object there is are some simple, safe and comprehensible ways of doing that. Take a look at std::stack http://www.sgi.com/tech/stl/stack.html or alternatively this answer https://stackoverflow.com/a/994925/453436 to What is Proxy Class in C++

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

Sidebar

Related Questions

Pretty simple question. I have a few ASP RequiredFieldValdators checking some text boxes. Out
Pretty simple question. I've got some Polygons and GroundOverlays defined in KML. Is there
This is a pretty general question, but the specific thing I'm doing is simple
My question is pretty simple, but I am having a hard time finding any
my question is pretty simple: can PHP and Java communicate, while PHP being the
My question is pretty simple: are gesture recognizers and KVOs cleared when UIViewController is
My question is pretty simple: How can I write a function once and make
I think the question is pretty simple, do I need all the rest of
Pretty simple question. I'm quite certain I have the Class, method, codebehind, etc linked
hopefully a pretty simple question this time. I have a Select method in a

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.