C++ 为函数调用了Move构造函数,但在C++;

C++ 为函数调用了Move构造函数,但在C++;,c++,return-value,move-semantics,rvalue-reference,rvalue,C++,Return Value,Move Semantics,Rvalue Reference,Rvalue,我不明白为什么在下面代码中的主函数期间调用move构造函数,特别是输出: FString::FString(string one) FString::FString(string two) FString::Move Constructor FString::Move Assign COMPLETE 因此,我关心的行是“FString::Move Constructor”-这对我来说意味着调用Move构造函数是为了实现函数GetStringTemp()的返回语句,但据我所知,NRVO应该意味着

我不明白为什么在下面代码中的主函数期间调用move构造函数,特别是输出:

FString::FString(string one)
FString::FString(string two)
FString::Move Constructor
FString::Move Assign
COMPLETE
因此,我关心的行是“FString::Move Constructor”-这对我来说意味着调用Move构造函数是为了实现函数GetStringTemp()的返回语句,但据我所知,NRVO应该意味着不应该调用Move构造函数。我是否误解了NVRO的行为?提前谢谢

#include <iostream>
#include <string>

class FString
{
    public:
        FString(std::string newstring)
        : _string(newstring)
        {
            std::cout << "FString::FString(string "+newstring+")" << std::endl;
        }

        FString(const FString& rhs) 
        {
            std::cout << "FString::Copy Constructor" << std::endl;
        }

        FString(FString&& rhs) 
        {
            std::cout << "FString::Move Constructor" << std::endl;
        }

        FString& operator=(const FString& rhs)
        {
            std::cout << "FString::Copy Assign" << std::endl;
            return *this;
        }

        FString& operator=(FString&& rhs)
        {
            std::cout << "FString::Move Assign" << std::endl;
            return *this;
        }

        void Print()
        {
            std::cout << "Printing: "+_string << std::endl;
        }

    private:
        std::string _string;
};

FString GetTempString()
{
    FString temp = FString("two"); // 2: Expected Constructor cout
    return temp; // No expected constructor as NVRO assumed
}

int main()
{
    FString myString = FString("one"); // 1: Expected Constructor cout
    myString = GetTempString(); // 3: Expected Move Assignment cout

    std::cout << "COMPLETE" << std::endl;
}
#包括
#包括
类F字符串
{
公众:
FString(std::string新闻字符串)
:_字符串(新闻字符串)
{

std::cout NRVO不保证。它是可选的,但不是必需的。NRVO不是强制性的。你用什么编译?gcc和clangmove构造。据我所知,只有RVO(不是NRVO)是C++17标准要求的。啊哈,是的,我是用Microsoft cl编译器编译的,所以这可能是因为RVO实际上不是必需的!@MattNewcombe“所以可能是因为RVO实际上不是必需的!”不要混淆RVO和NRVO。从C++17开始(由于MSV默认为C++14,您是否在启用它的情况下编译)只需要RVO,而不是NRVO。NRVO不保证。它是可选的,但不是必需的。NRVO不是必需的。您使用什么进行编译?gcc和clang move构造。据我所知,只有RVO(不是NRVO)是C++17标准要求的。啊哈,是的,我是用Microsoft cl编译器编译的,所以这可能是因为RVO实际上不是必需的!@MattNewcombe“所以可能是因为RVO实际上不是必需的!”不要混淆RVO和NRVO。从C++17开始(由于MSV默认为C++14,您是否在启用它的情况下编译)只需要RVO,而不需要NRVO。