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

Ahmed-Anas

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;
Mike Seymour

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);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Implicit integer type conversion in C

From Dev

Data Conversion from one class type to another class type

From Dev

C# implicit conversion and conversion from Object

From Dev

C# implicit conversion and conversion from Object

From Dev

Implicit conversion from enumeration type with Xcode 6

From Dev

How to resolve implicit conversion differences between SQL Server and .NET when converting from string to another data type

From Dev

C++ constructor implicit type conversion

From Dev

explicit/implicit type conversion c++

From Dev

C++ implicit conversion of pointer type

From Dev

explicit/implicit type conversion c++

From Dev

c++ implicit type conversion string -> int?

From Dev

Implicit conversion from user-defined type to primitive type in C++

From Dev

Implicit conversion from user-defined type to primitive type in C++

From Dev

C++ Implicit conversion from bool to string

From Dev

How to enable implicit type conversion from non-const to const in C++?

From Dev

C++ Why use an implicit conversion from (std::string) to (void) type?

From Dev

Implicit conversion from data type varchar to varbinary(max) is not allowed c#

From Dev

Implicit conversion for generic type?

From Dev

Swift implicit conversion of type

From Dev

Implicit conversion of function type

From Dev

Implicit type conversion with structs

From Dev

Implicit type conversion with structs

From Dev

Scala: Implicit Conversion From Generic Type to Second Generic Type

From Dev

C++ moving characters from one vector to another

From Dev

Moving from a vector with one allocator to a vector with another

From Dev

Copy items from one vector to another vector

From Dev

Copy items from one vector to another vector

From Dev

Implicit conversion from lambda expression to user-defined type

From Dev

implicit conversion from class to enumeration type in switch conditional

Related Related

  1. 1

    Implicit integer type conversion in C

  2. 2

    Data Conversion from one class type to another class type

  3. 3

    C# implicit conversion and conversion from Object

  4. 4

    C# implicit conversion and conversion from Object

  5. 5

    Implicit conversion from enumeration type with Xcode 6

  6. 6

    How to resolve implicit conversion differences between SQL Server and .NET when converting from string to another data type

  7. 7

    C++ constructor implicit type conversion

  8. 8

    explicit/implicit type conversion c++

  9. 9

    C++ implicit conversion of pointer type

  10. 10

    explicit/implicit type conversion c++

  11. 11

    c++ implicit type conversion string -> int?

  12. 12

    Implicit conversion from user-defined type to primitive type in C++

  13. 13

    Implicit conversion from user-defined type to primitive type in C++

  14. 14

    C++ Implicit conversion from bool to string

  15. 15

    How to enable implicit type conversion from non-const to const in C++?

  16. 16

    C++ Why use an implicit conversion from (std::string) to (void) type?

  17. 17

    Implicit conversion from data type varchar to varbinary(max) is not allowed c#

  18. 18

    Implicit conversion for generic type?

  19. 19

    Swift implicit conversion of type

  20. 20

    Implicit conversion of function type

  21. 21

    Implicit type conversion with structs

  22. 22

    Implicit type conversion with structs

  23. 23

    Scala: Implicit Conversion From Generic Type to Second Generic Type

  24. 24

    C++ moving characters from one vector to another

  25. 25

    Moving from a vector with one allocator to a vector with another

  26. 26

    Copy items from one vector to another vector

  27. 27

    Copy items from one vector to another vector

  28. 28

    Implicit conversion from lambda expression to user-defined type

  29. 29

    implicit conversion from class to enumeration type in switch conditional

HotTag

Archive