小编use*_*108的帖子

不对齐警告修复它们或不修复它们?

在我的自定义.zip解压缩器中,我得到这样的警告:

runtime error: load of misaligned address 0x7f280ffe875a for type 'const uint32_t', which requires 4 byte alignment
Run Code Online (Sandbox Code Playgroud)

究其原因是reinterpret_castuint32_t*和相同的解引用.我知道,memcpyuint32_t会解决这个问题,但我要这么做?我看到许多广泛使用的库(如pixman,cairo ......)都有同样的警告.

c++ memory-alignment type-punning

-1
推荐指数
1
解决办法
289
查看次数

可变参数函数模板中的:: std :: pair

是否可以编写可变参数函数模板,接受::std::pairs,而无需手动构造对:

void print_pairs()
{
}

template <typename ...B>
void print_pairs(::std::pair<int, int> const& a, B&&... b)
{
  ::std::cout << a.first << " " << a.second << ::std::endl;

  print_pairs(::std::forward<B>(b)...);
}
Run Code Online (Sandbox Code Playgroud)

但我希望能够写作print_pairs({1, 1}, {2, 2});,而不是一直::std::make_pair都在使用.

编辑:

经过一番思考,最好的解决方案可能是旧学校:

print_pairs(::std::pair<int, int> const&);
print_pairs(::std::pair<int, int> const&, ::std::pair<int, int> const&);
print_pairs(::std::pair<int, int> const&, ::std::pair<int, int> const&, ::std::pair<int, int> const&);
Run Code Online (Sandbox Code Playgroud)

等等...

c++ c++11

-2
推荐指数
1
解决办法
663
查看次数

交换基本类型的值

我想我们都知道如何交换值,但还有另一种方法:

constexpr auto assign(auto& ...a) noexcept
{
  return [&](auto const ...v) noexcept { ((a = v), ...); };
}
Run Code Online (Sandbox Code Playgroud)

要交换,我们需要调用assign(a, b)(b, a).

这是将值交换为基本类型的有效方法吗?它是否比通常的方式提供了更多的优化空间?

c++ swap

-4
推荐指数
1
解决办法
298
查看次数

goto或throw,优点/缺点

我有这段代码:

try
{
  // ...do something... possibly goto error
}
catch (...)
{
}
error:
// ...process error...
Run Code Online (Sandbox Code Playgroud)

我面临的问题是我是否应该使用goto(如果可能)或throw跳转到error标签.两种方法的(dis)优势是什么?

编辑:修改代码以符合标准.

c++

-5
推荐指数
1
解决办法
718
查看次数

标签 统计

c++ ×4

c++11 ×1

memory-alignment ×1

swap ×1

type-punning ×1