2

How can I cast vector<int> to const vector<const int>?

I've tried static_cast<const vector<const int>>(array) and using the value without a cast. Neither compiled.

noɥʇʎԀʎzɐɹƆ
  • 9,967
  • 2
  • 50
  • 67

2 Answers2

3

You cannot cast a std::vector<int> to const std::vector<const int>.

Besides, it does not make sense to use a std::vector<const int> at all. It doesn't give you any more safety than a const std::vector<int>.

Not only that, C++ does not allow construction of std::vector<const T>. See Does C++11 allow vector<const T>? for more info.

Community
  • 1
  • 1
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • ...can a `vector` even _have_ `const`-qualified element type? It's never worked for me when I tried in the dim and distant past, and it doesn't work for me now with `int const`. – underscore_d Jul 08 '16 at 23:01
  • 1
    @underscore_d, g++ does not allow creating a variable with `std::vector`. I don't know whether there is anything in the standard that prohibits such a variable. – R Sahu Jul 08 '16 at 23:06
  • I thought maybe it was to do with non-availability of copy/move ctors in the element types for which I tried it before, meaning reallocations and sorts and etc would need to use `operator=`, which obviously isn't `const`... but `int` has those, which torpedoes my theory. Sigh - off to the Standard I go. – underscore_d Jul 08 '16 at 23:08
  • @underscore_d a `const int` doesn't have `operator=` available to it. – R Sahu Jul 08 '16 at 23:10
  • To clarify, I was referring to it having the ctors, not assignment. – underscore_d Jul 08 '16 at 23:11
  • [Does C++11 allow vector?](http://stackoverflow.com/questions/6954906/does-c11-allow-vectorconst-t), to which the answer from a very reliable source appears to be a distinct _no_. The real fleshing-out is in the comments, btw. – underscore_d Jul 08 '16 at 23:14
1

You cannot change the type of the elements of the vector, but you can change the type of the vector to const std::vector<int> which will prevent making changes to the vector.

#include <vector>

int main()
{
    std::vector<int> v1;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);

    const std::vector<int>& v2 = v1;
    v2.push_back(4); // Causes an error
    v2[0] = 4; // Causes an error
    return 0;
}
Alden
  • 2,229
  • 1
  • 15
  • 21
  • Although it doesn't solve my caching problem, I'll leave it here because it might help others – noɥʇʎԀʎzɐɹƆ Jul 08 '16 at 22:24
  • This makes the _container_ const, but I'm guessing that by trying to use the disallowed concept of 'a container with `const` _elements_', OP hoped to be able to add to/reallocate the container (non-`const`) while not being able to alter elements after construction (`const`). One might assume this were possible post-C++11 as the compiler could use copy and move constructors for elements instead of assignment - but in reality, containers were never intended to have `const` elements - and currently, an incompatible default allocator prevents that anyway: http://stackoverflow.com/questions/6954906 – underscore_d Jul 08 '16 at 23:32