转换谓词,通过引用返回局部变量

Transform predicate returning local variable by reference

本文关键字:引用 返回 局部变量 谓词 转换      更新时间:2023-10-16

在以下代码中,我试图将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

为什么会这样?如何修复?

编译器是正确的。在lambda的返回类型中:

decltype(*std::begin(u))

std::vector<T>::begin()返回的迭代器进行解引用,根据uconst限定返回T&T const&。其次,lambda显示未定义行为,因为您正在返回对临时val的引用。

要解决您的问题,可以使用std::decay_t

std::decay_t<decltype(*std::begin(u))>

这导致我们:

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

您实际寻找的是vector的范围构造函数:

std::vector<char> buf(10, 10);
std::vector<uint8_t> u(buf.begin(), buf.end());

这比包含transform()的解决方案更有效,更容易阅读。