1

I have below function definition.

struct foo{
    vector<int> m_vec;
    vector<int>& getVec1()
    {
        return m_vec;
    }
    vector<int> getVec2() &
    {
        return m_vec;
    }
    vector<int>&& getVec3()
    {
        return std::move(m_vec);
    }
    vector<int> getVec4() && 
    {
        return std::move(m_vec);
    }
};  

int main()
{
    foo x;
    x.m_vec.push_back(1);
    std::cout << x.getVec1().size() << std::endl;
    x.m_vec.push_back(2);
    std::cout << x.getVec2().size() << std::endl;

    auto y = std::move(x).getVec3();
    std::cout << y.size() << std::endl;

    foo m;
    m.m_vec.push_back(1);
    auto z = std::move(m).getVec4();
    std::cout << z.size() << std::endl;

}

compile with: clang++ -std=c++11 -static -O3 main.cpp && ./a.out
output:
1
2
2
1

Below are my questions:
1. I do not know getVec2() function definition (what means that reference & after parenthese), is it same as getVec1()?
2. Is also same as getVec3() and getVec4() function?

Thanks in advance.

Jerry YY Rain
  • 4,134
  • 7
  • 35
  • 52

0 Answers0