samedi 11 mars 2017

ios_base::failure has no code()

I tried the code from cppreference.com but it seems e.code() is not there:

#include <iostream>
#include <system_error> // std::make_error_condition, std::ios_errc
int main () {
  std::cin.exceptions (std::ios::failbit|std::ios::badbit);
  try {
    std::cin.rdbuf(nullptr); // throws
  } catch (std::ios::failure& e) {
    std::cerr << "Fehler: ";
    if (e.code() ==         // <<< ERROR: no e.code() ???
       std::make_error_condition(std::io_errc::stream))
      std::cerr << "stream\n";
    else
      std::cerr << "other\n";
  }
}

My g++-6.2 and g++-5 and clang-3.9 (on linux) all say the same:

error: ‘class std::ios_base::failure’ has no member named ‘code’
     if (e.code() == std::make_error_condition(std::io_errc::stream))
           ^~~~

Even the unchanged example does not compile for me

#include <iostream>
#include <fstream>
int main()
{
    std::ifstream f("doesn't exist");
    try {
        f.exceptions(f.failbit);
    } catch (const std::ios_base::failure& e)
    {
        std::cout << "Caught an ios_base::failure.\n"
              << "Explanatory string: " << e.what() << '\n'
              << "Error code: " << e.code() << '\n';
    }
}

with

$ g++-6 etest.cpp -o etest.x
etest.cpp: In function ‘int main()’:
etest.cpp:12:42: error: ‘const class std::ios_base::failure’ has no member named ‘code’
                   << "Error code: " << e.code() << '\n';

I tried g++-6, g++-5, adding -std=c++1y, -std=c++98, same result. (the latter is ok, though, I believe).

Which is really odd, because the online compiler on the site does compile it.

The single hint I have from a run with -E (preprocessor):

class ios_base
{
# 246 "/usr/include/c++/6/bits/ios_base.h" 3
  public:
# 276 "/usr/include/c++/6/bits/ios_base.h" 3
  class failure : public exception
  {
  public:

This looks as if failure does indeed not derive from system_error, which would explain why there is no code(). But why? Its plain Ubuntu g++, its g++-6, its C++14... I have no compiler tweaks, links, hacks...

There seems to be some use of

#if _GLIBCXX_USE_CXX11_ABI

in the vicinity. But does that interfere? If so, how to do I make it un-interfere?

Does anyone have any idea what might be going on here?

Aucun commentaire:

Enregistrer un commentaire