Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

implicit conversion of vector from one type to another c++

Is it possible to convert a vector of one type to another implicitly?

i.e some way to make this code work (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;
like image 622
Ahmed-Anas Avatar asked May 28 '15 13:05

Ahmed-Anas


People also ask

What is implicit conversion in C++?

Implicit conversions. For built-in numeric types, an implicit conversion can be made when the value to be stored can fit into the variable without being truncated or rounded off. For integral types, this means the range of the source type is a proper subset of the range for the target type.

What is type conversion in C with example?

C Type Conversion – Implicit & Explicit Type Conversion in C. When variables and constants of different types are combined in an expression then they are converted to same data type. The process of converting one predefined type into another is called type conversion.

What are the rules for explicit type conversion?

Explicit type conversion requires a type casting operator. Keep in mind the following rules for programming practice when dealing with different data type to prevent from data loss : Integers types should be converted to float. Float types should be converted to double.

What is implicit type conversion or type promotion?

When the type conversion is performed automatically by the compiler without programmers intervention, such type of conversion is known as implicit type conversion or type promotion. The compiler converts all operands into the data type of the largest operand. The sequence of rules that are applied while evaluating expressions are given below:


2 Answers

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

You could initialise it from an iterator range:

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

perhaps 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);
like image 167
Mike Seymour Avatar answered Oct 25 '22 09:10

Mike Seymour


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_vectors are vectors that auto convert from other magic_vectors.

If you have a pointer to a magic_vector that you convert into a pointer to vector, then delete it as a vector, the result is undefined behavior. (However in practice, there will be no harm in every C++ implementation I have checked). This is, however, a strange way to act with vectors.

Replace use of vector with magic_vector. So long as you don't have specializations on the exact type of a container in your code, it should be a drop-in replacement, except now it will auto-convert between them.

Work could be done to have magic_vectors auto-convert with vectors instead of just with magic_vectors.

like image 29
Yakk - Adam Nevraumont Avatar answered Oct 25 '22 07:10

Yakk - Adam Nevraumont