创建一个rvalue并通过转发将其传递给一个函数

sma*_*c89 1 c++ debugging reference c++11

我有这个代码似乎工作,但我不确定我是否只是看到未定义的行为或它实际上工作.

#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;
}
Run Code Online (Sandbox Code Playgroud)

它能做什么:

所有这一切都是将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
Run Code Online (Sandbox Code Playgroud)

不,这不是一个错误,它确实错过了输出中的第一个字符,即使我没有看到如何,因此我更确切地知道有些事情我做错了.

我的担忧:

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

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

编译:

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

hof*_*ale 5

std::accumulate,std::ostringstream和标准

std::accumulate要求T(返回类型和初始值的类型)是CopyAssignableCopyConstructible.std::ostringstream既不是也不CopyAssignableCopyConstructible,所以就标准而言,这是不允许的.(事实上​​,MSVC会在你的脸上引起一个大红色错误!).

有趣的是,GCC,clang和icc(我测试过的)都没有产生警告(-pedantic尽管我不知道这是一个错误(不会警告std::ostringstream不存在CopyAssignable或是CopyConstructible)或一个功能(他们是否明确支持)仅限移动类型std::accumulate?).

无论如何,即使他们支持它,它也不符合标准,因此他们可以在技术上按照自己的意愿行事.

TLDR:根据标准不允许.

履行

std::forward这段代码没有必要.

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

在第二种用法中,您已经accum通过调用明确地转换为右值引用std::move.

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&)
Run Code Online (Sandbox Code Playgroud)

此外,考虑更换std::endl'\n',除非你明确要刷新流.