在以下代码中,我尝试将std::vector<char>
复制到std::vector<uint8_t>
。为了避免编译器警告,我在std::transform
的谓词中进行显式转换而不是使用std::copy
,就像这样
std::vector<char> buf(10, 10);
std::vector<uint8_t> u(10);
std::transform(std::begin(buf), std::end(buf), std::begin(u),
[](uint8_t val)->decltype(*std::begin(u)){
return static_cast<decltype (*std::begin(u))> (val);
});
对于这段代码,我收到编译器警告说我试图通过引用返回一个局部变量val
。
为什么会这样,以及如何解决?
答案 0 :(得分:5)
编译器是right。在lambda的返回类型中:
decltype(*std::begin(u))
取消引用std::vector<T>::begin()
返回的迭代器会返回T&
或T const&
,具体取决于const
u
的{{1}}资格。其次,您的lambda展示未定义的行为,因为您要返回对临时val
的引用。
要解决您的问题,您可以使用std::decay_t
std::decay_t<decltype(*std::begin(u))>
leads我们:
std::transform(std::begin(buf), std::end(buf), std::begin(u),
[](auto val)-> std::decay_t<decltype(*std::begin(u))> {
return val;
});
如果类型是可隐式转换的,您只需执行以下操作:
std::vector<uint8_t> u(std::begin(buf), std::end(buf));
或已经创建的u
:
u.assign(std::begin(buf), std::end(buf));
另一方面,对于其他一些复杂类型,普通循环也不错:
for(const auto& k : buf)
u.emplace( k.converted() ); //Assuming you must call a conversion function
答案 1 :(得分:2)
What you're actually looking for is the range constructor of vector
:
std::vector<char> buf(10, 10);
std::vector<uint8_t> u(buf.begin(), buf.end());
This is both more efficient and easier to read than the solution involving transform()
.