Home > OS >  How to insert elements of Enum type into vector of type uint8_t?
How to insert elements of Enum type into vector of type uint8_t?

Time:11-18

I am very new to C programming. I want to insert elements of type enum into a vector<uint8_t> ? ie append all elements of std::vector <ValType> call to std::vector<uint8_t> bravo .Is there any way to do so?

#include <stdio.h>
#include <vector>
#include <cstdint>

enum class ValType : uint8_t
{

    Working = 1,
    Failed = 0,
    Freezed = 0

};

int main()
{
    std::vector<uint8_t> bravo = {23, 23, 23, 22, 5};
    std::vector<ValType> call;
    bravo.insert(bravo.end(), call.begin(), call.end());

    return 0;
}

Live Here

I am getting an error while compiling :

   In file included from c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\vector:66,
                 from custom.cpp:2:
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\stl_uninitialized.h: In instantiation of '_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<ValType*, std::vector<ValType> >; _ForwardIterator = unsigned char*]':
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\stl_uninitialized.h:333:37:   required from '_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<ValType*, std::vector<ValType> >; _ForwardIterator = unsigned char*; _Tp = unsigned char]'
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\vector.tcc:751:34:   required from 'void std::vector<_Tp, _Alloc>::_M_range_insert(std::vector<_Tp, _Alloc>::iterator, _ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = __gnu_cxx::__normal_iterator<ValType*, std::vector<ValType> >; _Tp = unsigned char; _Alloc = std::allocator<unsigned char>; std::vector<_Tp, _Alloc>::iterator = std::vector<unsigned char>::iterator]'
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\stl_vector.h:1665:19:   required from 'void std::vector<_Tp, _Alloc>::_M_insert_dispatch(std::vector<_Tp, _Alloc>::iterator, _InputIterator, _InputIterator, std::__false_type) [with _InputIterator = __gnu_cxx::__normal_iterator<ValType*, std::vector<ValType> >; _Tp = unsigned char; _Alloc = std::allocator<unsigned char>; std::vector<_Tp, _Alloc>::iterator = std::vector<unsigned char>::iterator]'
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\stl_vector.h:1383:22:   required from 'std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::const_iterator, _InputIterator, _InputIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<ValType*, std::vector<ValType> >; <template-parameter-2-2> = void; _Tp = unsigned char; _Alloc = std::allocator<unsigned char>; std::vector<_Tp, _Alloc>::iterator = std::vector<unsigned char>::iterator; std::vector<_Tp, _Alloc>::const_iterator = std::vector<unsigned char>::const_iterator]'  
custom.cpp:18:17:   required from here
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\stl_uninitialized.h:138:72: error: static assertion failed: result type must be constructible from value type of input range
  138 |       static_assert(is_constructible<_ValueType2, decltype(*__first)>::value,
      |                                                                        ^~~~~
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\stl_uninitialized.h:138:72: note: 'std::integral_constant<bool, false>::value' evaluates to false
In file included from c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\vector:60,
                 from custom.cpp:2:
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\stl_algobase.h: In instantiation of 'static _OI std::__copy_move<false, false, std::random_access_iterator_tag>::__copy_m(_II, _II, _OI) [with _II = ValType*; _OI = unsigned char*]':
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\stl_algobase.h:495:30:   required from '_OI std::__copy_move_a2(_II, _II, _OI) [with bool _IsMove = false; _II = ValType*; _OI = unsigned char*]'
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\stl_algobase.h:522:42:   required from '_OI std::__copy_move_a1(_II, _II, _OI) [with bool _IsMove = false; _II = ValType*; _OI = unsigned char*]'
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\stl_algobase.h:530:31:   required from '_OI std::__copy_move_a(_II, _II, _OI) [with bool _IsMove = false; _II = __gnu_cxx::__normal_iterator<ValType*, std::vector<ValType> >; _OI = __gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned char> >]'
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\stl_algobase.h:620:7:   required from '_OI std::copy(_II, _II, _OI) [with _II = __gnu_cxx::__normal_iterator<ValType*, std::vector<ValType> >; _OI = __gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned char> >]'
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\vector.tcc:744:16:   required from 'void std::vector<_Tp, _Alloc>::_M_range_insert(std::vector<_Tp, _Alloc>::iterator, _ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = __gnu_cxx::__normal_iterator<ValType*, std::vector<ValType> >; _Tp = unsigned char; _Alloc = std::allocator<unsigned char>; std::vector<_Tp, _Alloc>::iterator = std::vector<unsigned char>::iterator]'
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\stl_vector.h:1665:19:   required from 'void std::vector<_Tp, _Alloc>::_M_insert_dispatch(std::vector<_Tp, _Alloc>::iterator, _InputIterator, _InputIterator, std::__false_type) [with _InputIterator = __gnu_cxx::__normal_iterator<ValType*, std::vector<ValType> >; _Tp = unsigned char; _Alloc = std::allocator<unsigned char>; std::vector<_Tp, _Alloc>::iterator = std::vector<unsigned char>::iterator]'
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\stl_vector.h:1383:22:   required from 'std::vector<_Tp, _Alloc>::iterator std::vector<_Tp, _Alloc>::insert(std::vector<_Tp, _Alloc>::const_iterator, _InputIterator, _InputIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<ValType*, std::vector<ValType> >; <template-parameter-2-2> = void; _Tp = unsigned char; _Alloc = std::allocator<unsigned char>; std::vector<_Tp, _Alloc>::iterator = std::vector<unsigned char>::iterator; std::vector<_Tp, _Alloc>::const_iterator = std::vector<unsigned char>::const_iterator]'  
custom.cpp:18:17:   required from here
c:\program files (x86)\mingw-w64\i686-8.1.0-posix-dwarf-rt_v6-rev0\mingw32\include\c  \11.2.0\bits\stl_algobase.h:385:25: error: cannot convert 'ValType' to 'unsigned char' in assignment
  385 |               *__result = *__first;
      |               ~~~~~~~~~~^~~~~~~~~~

Can someone please show me the correct way of doing this?

CodePudding user response:

You can use std::transform with your own conversion function.

std::vector<uint8_t> bravo = {23, 23, 23, 22, 5};
std::vector<ValType> call;

std::transform(bravo.cbegin(), bravo.cend(), std::back_inserter(call),
               [](uint8_t a) { return static_cast<ValType>(a); });

CodePudding user response:

This is explained in e.g. this scope enumeration reference:

There are no implicit conversions from the values of a scoped enumerator to integral types, although static_cast may be used to obtain the numeric value of the enumerator.

[Emphasis mine]

So while it might look like inheritance when defining the enumeration, it's not. The type ValType is a completely separate type, which can't be converted to or from any other plain integer type, not even the one used as the enumeration base-type.

That means you can't simply copy from a vector of ValType elements to a vector of uint8_t elements. If you need to do such a copy you must implement your own conversion (using e.g. static_cast), perhaps using std::transform and an back insert iterator.

  • Related