Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Treat vector<int*> as vector<const int*> without copying (C++0x)

A class contains a std::vector<int*>. External code needs read-only access to this vector, should not be able to modify the contents (neither the pointers or their contents). Inside the class, the values may change (e.g. double_values(), and so storing them as a std::vector<const int*> is not possible.

Is there a way to return the std::vector<int*> as a std::vector<const int*> without making a copy? It feels like there should be, because const is simply operating at compile time to say what can and cannot be modified.

Code: (compile with g++ -std=c++0x)

class ReadOnlyAccess
{
public:
  ReadOnlyAccess(const std::vector<int*> & int_ptrs_param):
    int_ptrs(int_ptrs_param)
  {
  }
  const std::vector<int*> & get_int_ptrs() const
  {
    return int_ptrs;
  }
  std::vector<const int*> safely_get_int_ptrs() const
  {
    // will not compile (too bad):
    //    return int_ptrs;

    // need to copy entire vector
    std::vector<const int*> result(int_ptrs.size());
    for (int k=0; k<int_ptrs.size(); k++)
      result[k] = int_ptrs[k];
    return result;
  }
  void double_values()
  {
    for (int*p : int_ptrs)
      *p *= 2;
  }
  void print() const
  {
    for (const int * p : int_ptrs)
      std::cout << *p << " ";
    std::cout << std::endl;
  }
private:
  std::vector<int*> int_ptrs;
};

int main() {
  ReadOnlyAccess roa(std::vector<int*>{new int(10), new int(20), new int(100)});
  std::vector<const int*> safe_int_ptrs = roa.safely_get_int_ptrs();
  // does not compile (good)
  // *safe_int_ptrs[0] = -100000;
  roa.print();

  const std::vector<int*> & int_ptrs = roa.get_int_ptrs();
  // changes are made to the internal class values via the accessor! nooooo!
  *int_ptrs[0] = -100000;
  roa.print();

  return 0;
}
like image 969
user Avatar asked Apr 22 '12 06:04

user


2 Answers

Returning the vector will imply a copy if you want to keep the const pointers anyway.

However, if your goal is to provide a way to use the values without modifying them, or modifying it's container, then a visitor pattern based algorithm might be a very good solution, in particular now that we can use lambda expressions:

#include <vector>
#include <iostream>

class Data
{
public:

    //...whatever needed to fill the values

    // here we assume that Func is equivalent to std::function< void ( int )> or std::function< void (const int& ) > and can return anything that will be ignored here.
    template< class Func > 
    void for_each_value( Func func ) const // read-only
    {
        for( const int* value : m_values ) // implicit conversion
        {
             func( *value ); // read-only reference (const &), or copy
             // if func needs to work with the adress of the object, it still can by getting a reference to it and using & to get it's adress
        }
    }


    void print() const
    {
        std::cout << "\nData values: \n";
        for_each_value( []( const int value ) { std::cout << "    "<< value << '\n'; } );
    }

    void count_values() const { return m_values.size(); }

private:

    std::vector<int*> m_values;

};



int main()
{
    Data data;
    // ... whatever needed to fill the data

    data.print();    

    std::vector<int> modified_values;
    data.for_each_value( [&]( int value ) { modified_values.push_back( value + 42 ); } );

    return 0;
}

If you understand that, and the different ways to use the values can be reduced to a few half-generic algorithms, then it will make your code simpler and allow you to keep data inside your structures instead of exposing it's the guts.

like image 138
Klaim Avatar answered Nov 08 '22 02:11

Klaim


You can provide a view to const values via custom iterators. An easy way would be to use boost::iterator:

#include <boost/iterator/indirect_iterator.hpp>

class ReadOnlyAccess
{
// ...
    typedef boost::indirect_iterator<const int* const*, const int> const_val_iter_type;
    const_val_iter_type cval_begin() {
        return it_t{const_cast<const int* const*>(&int_ptrs[0])};
    }
}

int main() {
    // ...
    auto x = roa.cval_begin();
    std::cout << x[0] <<' ' << x[1] << x[2] <<'\n';
    // we can still access the pointers themselves via .base() member function:
    for (int i=0; i<3; ++i)
        assert(x.base()[i] == safe_int_ptrs[i]);
    // the values are read-only, the following does not compile:
    // x[0] = -1;
    // **x.base() = -1;
    // *x.base() = nullptr;
}

If we used boost::indirect_iterator<typename std::vector<int*>::const_iterator, const int> for const_val_iter_type, we could modify the pointed values via .base() (but not directly like in e.g. x[0] = -1), so this solution is not general.

like image 1
rafak Avatar answered Nov 08 '22 00:11

rafak