1

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?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
towi
  • 21,587
  • 28
  • 106
  • 187
  • [Works for me](http://rextester.com/LTJFI43347). Must be something with your standard library installation. – Igor Tandetnik Mar 11 '17 at 23:55
  • Like cppreference.com shows, in C++11 `ios_base::failure` derives from `system_error` which contains the `code()` function. Before C++11 we didn't have a class `system_error`. So surely the use of a C++11 compatible ABI is important. – Bo Persson Mar 12 '17 at 02:17
  • Just having the same issue as the poster .. Ubuntu, g++ 5.4.1, using -std=c++14 flag. Code does not compile for me either. Message (shortened): `const class std::ios_base::failure’ has no member named ‘code’` – Andreas W. Wylach May 31 '17 at 11:22
  • 1
    @towi: A followup to my comment above, you may want to have a look at this post. It explains a bit more the problem. I use the way of the accepted answer for now. https://codereview.stackexchange.com/questions/57829/better-option-than-errno-for-file-io-error-handling – Andreas W. Wylach May 31 '17 at 13:14
  • @AndreasW.Wylach thanks for pointing that out. Alas, I can only +1 your comment, not "accept" it :-) – towi May 31 '17 at 13:50

0 Answers0