I'm interfacing std::vector
to a C program which needs to know the size in bytes.
Can I do this without the expression sizeof(vec[0])
?
void *dest = get_dest();
std::vector<T> vec;
memcpy(dest, vec.data(), vec.size() * sizeof(vec[0]));
Please do not offer C++ style alternatives to memcpy
.
template<class T, class A>
std::size_t bytes_in_vector( std::vector<T,A>const& v ){
return v.size()*sizeof(T);
}
template<class A>
std::size_t bytes_in_vector( std::vector<bool,A> const& )=delete;
however, there is little wrong with sizeof(vec[0])
so long as T
is not bool
. If T
is bool
you are not allowed to use memcpy
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.