Implicit conversion of a vector from one type to another C ++

Hey guys, hoping you could help me.

I want to know if it is possible to convert a vector of one type to another implicitly

Somehow do this code (obviously this is a simplified problem of what I am trying to do)

std::vector<int> intVec;
intVec.push_back(1);

std::vector<double> doubleVec = intVec;
std::vector<double> doubleVec2;
doubleVec2 = intVec;

      

+3


source to share


4 answers


No, there is no conversion (implicit or otherwise) between different types of vectors.

You can initialize it from a range of iterators:

std::vector<double> doubleVec(intVec.begin(), intVec.end());

      



maybe wrapping this in a function:

template <typename To, typename From>
To container_cast(From && from) {
    using std::begin; using std::end; // Koenig lookup enabled
    return To(begin(from), end(from));
}

auto doubleVec = container_cast<std::vector<double>>(intVec);

      

+11


source


One way is to create a transform function. This allows you to express intent on the calling site:

#include <iostream>
#include <vector>

template<class To, class From, class Allocator>
std::vector<To, typename Allocator::template rebind<To>::other>
implicit_convert(const std::vector<From, Allocator>& vf)
{
    return { std::begin(vf), std::end(vf) };
}

template<class To, class ToA, class From, class FromA>
void implicit_overwrite(std::vector<To, ToA>& to, const std::vector<From, FromA>& from)
{
    to.clear();
    std::copy(begin(from), end(from), back_inserter(to));
}

int main(int argc, const char * argv[]) {
    using namespace std;
    std::vector<int> vi { 1, 2 , 3 };
    auto vd = implicit_convert<double>(vi);

    cout << "after conversion\n";
    for (const auto& i : vd) {
        cout << i << endl;
    }

    vi.push_back(4);
    implicit_overwrite(vd, vi);
    cout << "after copy\n";
    for (const auto& i : vd) {
        cout << i << endl;
    }


    return 0;
}

      



expected output:

after conversion
1
2
3
after copy
1
2
3
4

      

+2


source


template<class T, class A=std::allocator<T>>
struct magic_vector:std::vector<T,A> {
  using base=std::vector<T,A>;
  using base::base;
  magic_vector(magic_vector const&)=default;
  magic_vector(magic_vector &&)=default;
  magic_vector& operator=(magic_vector const&)=default;
  magic_vector& operator=(magic_vector &&)=default;
  magic_vector()=default;

  template<class U, class B,
    class=typename std::enable_if<std::is_convertible<U,T>::value>::type
  >
  magic_vector( magic_vector<U,B> const& o ):
    base( o.begin(), o.end() )
  {}
  template<class U, class B,
    class=typename std::enable_if<
      std::is_convertible<U,T>::value
      && noexcept( T(std::declval<U&&>()) )
    >::type
  >
  magic_vector( magic_vector<U,B>&& o ):
    base(
      std::make_move_iterator(o.begin()),
      std::make_move_iterator(o.end())
    )
  {}
};

      

magic_vector

- vectors that are automatically converted from other magic_vector

s.

If you have a pointer to magic_vector

that you convert to a pointer to vector

, then delete it as vector

, the result is undefined behavior. (In practice, however, there won't be any harm in every C ++ implementation I've tested.) This is, however, an odd way to deal with vector

s.

Replace usage vector

with magic_vector

. As long as you don't have a specialization for a particular container type in your code, it should be a drop-in replacement, except that it will now automatically convert between the two.

One could do the work with magic_vector

auto-convert using vector

, not just magic_vector

s.

+2


source


you can go for something like this (assuming myclass is your class that can be built from std::pair

):

#include <iostream>
#include <vector>
#include <utility>

using std::cout;
using std::endl;

class myclass
{
public:
    myclass(const std::pair<int, int>& p): first_(p.first), second_(p.second) {}
    int first() {return first_;}
    int second() {return second_;}
private:
    int first_;
    int second_;
};

template <class T>
class myvector : public std::vector<T> 
{
    using base = std::vector<T>;
    using base::base;
};

template<>
class myvector<myclass> : public std::vector<myclass>
{
public:
    myvector(const std::vector<std::pair<int, int>>& vp):
std::vector<myclass>(vp.begin(), vp.end()) {}

};

int main()
{
    std::vector<std::pair<int, int>> vp {{12,3}, {1, 7}};
    myvector<myclass> mm = vp;
    cout<<mm[0].first(); //prints 12

}

      

You inherit myvector

from std::vector

and then specialize in myclass

. Alternatively you can define myvector

in a namespace and access it asmynamespace::vector<myclass>

+1


source







All Articles