mercredi 17 avril 2019

C++ regex_search breaks when compiled with -O1

Sample code:

#include <iostream>
#include <string>
#include <regex>

int main()
{
  std::regex npat(R"(^(\d+))");
  std::smatch m;
  std::regex_search(std::string("10"), m, npat);
  std::cout << m.size() << " m.str(1): |"
            << m.str(1) << "| ";
  std::cout << std::stoi(m.str(1)) << std::endl;
}

When compiled with

g++ -std=c++11 main.cpp

the output is

2 m.str(1): |10| 10

which is expected.

However, when compiled with

g++ -std=c++11 -O1 main.cpp

The output becomes

libc++abi.dylib: terminating with uncaught exception
of type std::invalid_argument: stoi: no conversion
2 m.str(1): || Abort trap: 6


Compiler version:

g++ -v

Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1 Apple LLVM version 10.0.1 (clang-1001.0.46.3) Target: x86_64-apple-darwin18.5.0 Thread model: posix InstalledDir: /Library/Developer/CommandLineTools/usr/bin

Aucun commentaire:

Enregistrer un commentaire