[英]Creating an rvalue and passing it to a function by forwarding
我有这段代码似乎可以正常工作,但是我不确定是否只是看到未定义的行为或它确实在工作。
#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 ++关键字的向量组合成一个字符串,并用|
分隔|
。
这是我运行它时的输出:
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
不,这不是一个错误,即使我看不到它,它的确确实缺少了输出中的第一个字符,因此,我更有理由确定自己做错了什么。
我特别担心它可能具有一些未定义的行为,因为该函数正在通过引用来操纵一个临时(ostringstream)。 最后返回... 右值引用 ?
在那儿,我需要帮助以了解如何确保我没有做错什么。 另外,通过使用转发和移动语义,我可以在其中进行任何改进吗? 建议离开。
编译时在GCC和Clang上没有警告或错误。
std::accumulate
, std::ostringstream
和标准 std::accumulate
要求T
(返回类型和初始值的类型)为CopyAssignable
和CopyConstructible
。 std::ostringstream
既不是CopyAssignable
也不是CopyConstructible
,所以就标准而言,这是不允许的。 (实际上,MSVC在您的面前抛出了一个大的红色错误!)。
有趣的是,尽管我不知道这是否是一个错误(不警告std::ostringstream
不是CopyAssignable
或CopyConstructible
),但GCC,clang或icc(我测试的-pedantic
)都不会产生警告(带有-pedantic
)。或功能(它们是否明确支持std::accumulate
的仅移动类型?)。
无论如何,即使他们支持它,也不在标准中,所以他们可以在技术上按照自己的意愿做。
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'
,除非您明确想要刷新流。
您的问题(损坏的第一个字符)可以减少为以下内容:
#include <string>
#include <sstream>
#include <iostream>
int main()
{
std::ostringstream myStream("constexpr", std::ostringstream::ate);
myStream = std::move(myStream);
std::cout << myStream.str() <<std::endl;
return 0;
}
在我看来,哪一个看起来像是GCC中的错误。
它在Visual Studio 2013中工作正常,但问题重现于
mingw-w64 \\ x86_64-7.2.0-posix-seh-rt_v5-rev0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.