如何将值插入另一个的“中间”?

时间:2019-04-28 09:03:48

标签: c++ arrays c++11 byte idiomatic

我分别具有类型T1和T2的两个值v1和v2,其中sizeof(T1)> sizeof(T2)。两种类型都是纯旧数据。现在,我想用v2的字节替换v1的第k,k + 1,... k + sizeof(T2)-1个字节。

C ++本身并没有在语言中提供此功能,也没有据我所知在标准库中(至少不是直接提供)。一般地实现此目标的最佳方法是什么?即实施:

template<typename T1, typename T2>
void replace_bytes(T1& v1, T2 v2, std::size_t k)

或至少

template<typename T1, typename T2, std::size_t k>
void replace_bytes(T1& v1, T2 v2)

我的想法是:

  1. 重新解释强制转换为字节数组
  2. 重新解释强制转换为std :: bytes数组
  3. 使用跨度
  4. 地址为v1的指针算术
  5. 对于不太大的类型-重新解释为无符号整数并使用位运算:与掩码,移位或进行“与”运算以组合现有位和替换位。

注意:

  • 当然,如果k太高,此处会存在UB(或者我们可以检查它是否太高)。
  • 为简单起见,您可以假定内存布局为低位字节序。
  • 如果对齐方式存在问题,请明确说明您的选择。
  • 效率/速度当然是关键问题。
  • 如果您的建议需要更新的C ++语言标准,那很好,但是请务必提及。
  • 在编译期间对代码进行优化是很重要的。

2 个答案:

答案 0 :(得分:2)

// needed include files
#include <iostream>  // for cout
#include <stdexcept> // for runtime_error
#include <cstring>   // for memcpy

// generic template function that takes 3 arguments
// 1 destination object
// 2 source object
// 3 from which byte to start in the destination
template<class T1, class T2>
void replace_bytes ( T1& t1, const T2& t2, std::size_t k )
{
// at compile time, store the size of T1 type in t1_size
   constexpr std::size_t t1_size = sizeof(T1);
// at compile time, store the size of T2 type in t2_size
   constexpr std::size_t t2_size = sizeof(T2);
// if we copy t2 bytes to t1, do we run out of memory ?
   if ( k + t2_size > t1_size )
   {
       throw std::runtime_error("Can't copy out of bounds.");
   }
// do the copying, casting is required for proper pointer arithmitic
   std::memcpy( (void*) (((char*)&t1)+k), (const void*) &t2, t2_size );
}

int main()
{
  int x = 0;
  char c = 10;
  replace_bytes(x, c, 0);
  std::cout << x << std::endl;
}

干净的代码版本(无注释):

#include <iostream>
#include <stdexcept>
#include <cstring>

template <class T1, class T2>
void replace_bytes ( T1& t1, const T2& t2, std::size_t k )
{
   constexpr std::size_t t1_size = sizeof(T1);
   constexpr std::size_t t2_size = sizeof(T2);

   if ( k + t2_size > t1_size )
   {
       throw std::runtime_error("Can't copy out of bounds.");
   }
   std::memcpy( (void*) (((char*)&t1)+k), (const void*) &t2, t2_size );
}

int main()
{
  int x = 0;
  char c = 10;
  replace_bytes(x, c, 0);
  std::cout << x << std::endl;
}

答案 1 :(得分:0)

以下内容适用于最大1字节的T1大小,以及seems to get optimized well enough在GCC,clang和MSVC上的工作-至少在内联时如此:

namespace detail {

template <unsigned NBytes> struct uint;

template<> struct uint<1> { using type = uint8_t;  };
template<> struct uint<2> { using type = uint16_t; };
template<> struct uint<4> { using type = uint32_t; };
template<> struct uint<8> { using type = uint64_t; };

} // namespace detail

template <unsigned NBytes>
using uint_t = typename detail::uint<NBytes>::type;

template <typename T1, typename T2>
inline void replace_bytes(T1& v1 ,T2 v2, std::size_t k)
{
    static_assert(sizeof(T1) > sizeof(T2), "invalid sizes");
    static_assert(std::is_trivial<T1>::value, "T1 must be a trivial type");
    static_assert(std::is_trivial<T2>::value, "T2 must be a trivial type");
    auto shift_amount = k * CHAR_BIT;
    using uint_1_t = uint<sizeof(v1)>;
    using uint_2_t = uint<sizeof(v2)>;
    auto& v1_as_uint = *reinterpret_cast<uint_1_t*>(&v1);
    const auto& v2_as_uint = *reinterpret_cast<uint_2_t*>(&v2);

    auto v1_mask = ~( (uint_1_t{1} << (sizeof(T2) * CHAR_BIT) - 1) << shift_amount);
    auto shifted_v2 = uint_1_t{v2_as_uint} <<  shift_amount;
    v1_as_uint = (v1_as_uint & v1_mask ) | shifted_v2;
}

但是我觉得最好避免超参数-实际上,这样做可以实现函数to be strictly in-registers

template <typename T1, typename T2>
T1 replace_bytes(T1 v1 ,T2 v2, std::size_t k)
{
    static_assert(sizeof(T1) > sizeof(T2), "invalid sizes");
    static_assert(std::is_trivial<T1>::value, "T1 must be a trivial type");
    static_assert(std::is_trivial<T2>::value, "T2 must be a trivial type");
    auto shift_amount = k * CHAR_BIT;
    using uint_1_t = uint_t<sizeof(v1)>;
    using uint_2_t = uint_t<sizeof(v2)>;
    auto& v1_as_uint = *reinterpret_cast<uint_1_t*>(&v1);
    const auto& v2_as_uint = *reinterpret_cast<uint_2_t*>(&v2);

    auto v1_mask = ~( (uint_1_t{1} << (sizeof(T2) * CHAR_BIT) - 1) << shift_amount);
    auto shifted_v2 = uint_1_t{v2_as_uint} <<  shift_amount;
    return (v1_as_uint & v1_mask ) | shifted_v2;
}
相关问题