0

I'm getting a compiler error https://godbolt.org/z/jEEs9Kcx6 when using emplace_back function to assign the returned reference to a reference variable in MSVC but not in GCC.

#include <iostream>
#include <string>
#include <vector>

int main() {
    std::vector<std::string> vecs;

    auto& str1 = vecs.emplace_back("Hello, world!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
    std::cout << str1 << std::endl;
}
Harry
  • 2,177
  • 1
  • 19
  • 33
  • @FrançoisAndrieux updated my post by providing link. – Harry Sep 02 '22 at 15:37
  • 1
    Your project is not compiling for C++17. On godbolt it compiles fine with `/std:c++17` https://godbolt.org/z/zWfsnrTMh – François Andrieux Sep 02 '22 at 15:38
  • 2
    GCC 12 uses C++17 by-default. MSVC does not. Add the correct flags and it will work: https://godbolt.org/z/o1M16vvYe (`emplace_back` only returns a reference since C++17) – user17732522 Sep 02 '22 at 15:38

0 Answers0