简体   繁体   中英

remove const qualifier of vector in c++

Is it possible to remove constness of vector? If so, how can it be achieved? For some reason, I don't have access to the main () , can I still remove the constness of arr in the following code snipet? It feels like that auto arr0 = const_cast<vector<int> & > (arr); is identical to vector<int> arr0 = arr; . I guess it is just an example of explicit vs implicit cast,which both creates a copy of original array ````arr```. Is it possible to remove constness inplace without creating a copy?

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;


vector<int> func(const vector<int>& arr){
//    vector<int> arr0 = arr;
    auto arr0 = const_cast<vector<int> & > (arr);
    sort(arr0.begin(), arr0.end());
    return arr0;
}

int main(){
    const vector<int> arr = {3,4,1,5,2};
    auto res = func(arr);
    for (auto n:res)
        cout<<n<<" ";
    return 0;

}

Don't attempt to cast away constness. Modifing a const object will result in undefined behaviour.

is identical to vector<int> arr0 = arr<\/code> ;

The cast is redundant.

If it is const, then you cannot modify it.

You can make a copy, it achieves what you're already trying to do. (You get return value optimization<\/a> , too).

#include <vector>
#include <algorithm>

template <class T>
static std::vector<T> func(std::vector<T> v) {
  std::sort(v.begin(), v.end());
  return v;
}

Your example is fine. And there is no need to make a copy. There is no undefined behavior. Here's why. This declares arr as a top level const. You may not modify the object without UB. However, the object that is const does not include the dynamic memory contents. In fact the contents are not even allowed to be declared const.

const vector<int> arr = {3,4,1,5,2};

You are allowed to cast away constness so long as you don't alter/write to the const arr object. Calling v.begin() and v.end() does not alter the arr object hence is not undefined behavior. I explored this here

This is not UB and sorts arr inline.

void func(const vector<int>& arr){
    auto& arr0 = const_cast<vector<int> & > (arr);
    sort(arr0.begin(), arr0.end());
}

Here's an example of the code in a constexpr function:

#include <vector>
#include <algorithm>

consteval int foo()
{
    const std::vector<int> a = { 1,2,3 };
    auto& v = const_cast<std::vector<int>&>(a);
    v[0] = 42;
    std::sort(v.begin(), v.end());
    return a[2];
}

int main()
{
    return foo();  // returns 42 since sorted
}

Compiler Explorer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM