gpt4 book ai didi

c++ - 创建右值并通过转发将其传递给函数

转载 作者:太空宇宙 更新时间:2023-11-03 10:24:04 24 4
gpt4 key购买 nike

我有这段代码似乎可以工作,但我不确定我是否只是看到了未定义的行为或者它实际上在工作。

#include <string>
#include <vector>
#include <sstream>
#include <numeric>
#include <iostream>

auto main() -> int {

const std::vector<std::string> keywords = {
"and","and_eq","asm","auto","bitand", "bitor","bool","break","case",
"catch","char","class","compl","const", "const_cast","continue",
"default","#define","delete","do","double","dynamic_cast","else","enum",
"explicit","export","extern", "extern \"C\"","false","float",
"for","friend","goto","if","inline","int","long","mutable","namespace",
"new","not","not_eq","operator","or", "or_eq","private","protected",
"public","register","reinterpret_cast","short","signed","sizeof",
"static","static_cast","struct","switch","template","this","throw",
"true","try","typedef","typeid","typename","union","unsigned","using",
"virtual","void","volatile","void","wchar_t","while","xor","xor_eq",
"return", "decltype"
};

std::ostringstream keywords_pattern =
std::accumulate(keywords.begin(), keywords.end(), std::forward<std::ostringstream>(
std::ostringstream("constexpr", std::ostringstream::ate)),
[](std::ostringstream &accum, const std::string& next) -> std::ostringstream&& {
accum << '|' << next;
return std::forward<std::ostringstream>(std::move(accum));
});

std::cout << keywords_pattern.str() << std::endl;
}

它的作用:

所有这一切都是将 C++ 关键字的 vector 组合成一个由 | 分隔的字符串。

输出:

这是我运行时的输出:

 onstexpr|and|and_eq|asm|auto|bitand|bitor|bool|break|case|catch|char|class|compl|const|const_cast|continue|default|#define|delete|do|double|dynamic_cast|else|enum|explicit|export|extern|extern "C"|false|float|for|friend|goto|if|inline|int|long|mutable|namespace|new|not|not_eq|operator|or|or_eq|private|protected|public|register|reinterpret_cast|short|signed|sizeof|static|static_cast|struct|switch|template|this|throw|true|try|typedef|typeid|typename|union|unsigned|using|virtual|void|volatile|void|wchar_t|while|xor|xor_eq|return|decltype

No 这不是一个错误,它确实缺少输出中的第一个字符,尽管我看不出是怎么回事,因此我更有理由确定我正在做某事错了。

我的顾虑:

我特别担心它可能有一些未定义的行为,因为该函数正在通过引用操纵一个临时的(ostringstream);并最终返回...一个右值引用??

这正是我需要帮助以了解如何确保我没有做错事情的地方。此外,通过使用转发和移动语义,我可以在那里做出任何改进吗?建议离开。

编译:

在 GCC 和 Clang 上编译时没有警告或错误。

最佳答案

std::accumulatestd::ostringstream 和标准

std::accumulate 要求 T(返回类型和初始值的类型)是 CopyAssignableCopyConstructible std::ostringstream 既不是CopyAssignable 也不是CopyConstructible,所以就标准而言,这是不允许的。 (实际上,MSVC 会在您面前抛出一个红色的大错误!)。

有趣的是,GCC、clang 和 icc(我测试过的 ons)都不会产生警告(使用 -pedantic),尽管我不知道这是否是一个错误(不警告std::ostringstream 不是 CopyAssignableCopyConstructible)或功能(它们是否明确支持 std::的移动类型)积累?).

不管怎么说,即使他们支持它,它也不在标准中,所以他们在技术上可以随心所欲地做。

TLDR:根据标准,这是不允许的。

实现

这段代码中不需要std::forward

在第一次使用中,std::ostringstream 构造函数已经返回一个右值引用。

在第二种用法中,您已经通过调用 std::move 显式地将 accum 转换为右值引用。

std::forward 在模板中很有用,其中推导出要转发的变量的类型,因此您不知道它是否可以/应该移动或复制。示例:

template<typename T>
void do_something(T&& thing) {
call_impl(std::forward<T>(thing));
}

do_something(std::string("text")); // calls call_impl(std::string&&);

std::string example{"text"};
do_something(example); // calls call_impl(std::string&) or call_impl(const std::string&)

此外,考虑将 std::endl 替换为 '\n',除非您明确想要刷新流。

关于c++ - 创建右值并通过转发将其传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46848074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com