6

I am using Codeblocks 17.12 and have already set compiler settings to C++11 standard. I am studying from Bjarne Stroustrup's book "Programming - Principles and Practice using C++". In his book he asked to include "std_lib_facilities.h". I copied it from his website and saved in "include" folder of "Mingw" folder. After that I proceeded to make a simple program:

#include<iostream>
#include "std_lib_facilities.h"
main()
{
    std::cout<<"Hello world";
}

But the compiler is showing following errors and warnings:

 warning: This file includes at least one deprecated or antiquated header which may be removed without further notice at a future date.  
 Please use a non-deprecated interface with equivalent functionality instead. 
 For a listing of replacement headers and interfaces, consult the file backward_warning.h. To disable this warning use -Wno-deprecated. [-Wcpp]

 error: template-id 'do_get<>' for 'String > 
   std::__cxx11::messages<char>::do_get(std::messages_base::catalog, int, int, const String&) const' does not match any template declaration

 note: saw 1 'template<>', need 2 for specializing a member function template

Also the error which is showing is in the 1971 line of the header file "locale_facets_nonio.h".
I tried to find out the solution to this problem in other forums, but could not find a satisfactory answer.
Some are saying we should not use this file "std_lib_facilities.h" at all as it is using deprecated or antiquated headers.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
RSSB
  • 144
  • 2
  • 4
  • 14
  • 1
    I personally would not recommend learning C++ from that book - it doesn't seem to be properly proof-read, particularly the code, and Stroustrup is not the greatest of teachers, despite his other obvious gifts. –  Jul 12 '18 at 20:10
  • @Neil Then which book will you recommend – RSSB Jul 12 '18 at 20:12
  • I would not recommend learning C++ as a first language. –  Jul 12 '18 at 20:14
  • Seconded -- I've been teaching out of this book for a few years, and I'd love to find a replacement (and I don't want to write my own) – Chad Jul 12 '18 at 20:14
  • Well I have already studied a bit of C and I knew that is more proper here but since the author is using this "std_lib_facilities.h" a lot so I thought that it would be better to go the way the author wants me to go but since you are saying that I should find a better book I please request you to enlighten me on this as I don't know which book will be better @Neil – RSSB Jul 12 '18 at 20:19
  • I would recommend Accelerated C++ or C++ Primer. Unfortunately, the number of good introductory modern C++ books is very low (about zero), but these will get you a long way in there for older C++, which is still applicable, and you can pick them up cheap S/H. There are a lot of very good advanced books . –  Jul 12 '18 at 20:21
  • 2
    @NeilButterworth _"I would not recommend learning C++ as a first language"_ I have to counter discourse about that: There's nothing wrong with learning C++ as a 1st programming language, as long that's not completely gets confused with irrelevant C language idioms in 1st place. Biggest failure in common academia plans as most commonly seen here. – πάντα ῥεῖ Jul 12 '18 at 20:34
  • @πάντα ῥεῖ There is nothing "wrong" with it, but I would always recommend learning an interpreted language first, if for no other reason than that was what I did, and it seems to have worked :-) –  Jul 12 '18 at 20:47
  • @Neil Well I've been starting out with BASIC, later assembly and C language. When I met C++ that was boosting my productivity a lot. Thus I've started with an _interpreted language_, and soon started to abuse it to run assembly level code. If I'd known some better idioms at that time, I'd decided for C++ even earlier, but it was all at it is beginnings that time (long ago). – πάντα ῥεῖ Jul 12 '18 at 21:02
  • @πάντα ῥεῖ Almost exactly my history too (throw in a bit of FORTH and FORTRAN) and I think you really are confirming what I said. Start out with an interpreted language - these days Python. I don't see how using it run assembler is abusing it. –  Jul 12 '18 at 21:06
  • @Neil _" I don't see how using it run assembler is abusing it."_ Because you don't actually want to write that kind of sandbox debugger in BASIC code ;) – πάντα ῥεῖ Jul 12 '18 at 21:10
  • I also started using this book and ran into some problems with this custom header in chapter 4 (he uses a nasty macro hack that impacts vectors - he even calls it "disgusting" in his comments). [It's recommended for learning the language in the definitive list!](https://stackoverflow.com/a/388282/9760446) <-- that's why I bought it. I *really* wish *Accelerated C++* was updated for C++11. – Arthur Dent Jul 12 '18 at 21:31
  • 1
    If you're going to get C++ Primer, be careful not to get C++ Primer Plus. It's an entirely different book which is bad like many others. – eesiraed Jul 12 '18 at 23:27

6 Answers6

11

There is an updated version of that file that works fine for the most recent revision of the ISO/IEC 14882 standard, namely C++17.

https://github.com/BjarneStroustrup/Programming-_Principles_and_Practice_Using_Cpp/blob/master/std_lib_facilities.h

You don't need that line:

#include<iostream> 

Hope you have not quit learning C++ with that wonderful book!

Gilberto Albino
  • 2,572
  • 8
  • 38
  • 50
4

we should not use this file "std_lib_facilities.h" at all as it is using deprecated or antiquated headers.

You should #include standard headers as you use them. The std_lib_facilities.h might get out of sync.

#include<iostream>
#include "std_lib_facilities.h"
int main() {
    std::cout<<"Hello world";
}

should rather be

#include<iostream>
// #include "std_lib_facilities.h" Remove this entirely!
int main() {
    std::cout<<"Hello world";
}

Using more standard features like std::string should be:

#include<iostream>
#include<string>
int main() {
    std::string hello = "Hello world";
    std::cout<<hello;
}

Extending further, reading the #include std_lib_facilities.h in your books example should probably become to expand the actually necessary standard header includes for your compilable and productive code.
Here's just a default starting template as used by Coliru

#include <iostream>
#include <vector>

template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& vec)
{
    for (auto& el : vec)
    {
        os << el << ' ';
    }
    return os;
}

int main()
{
    std::vector<std::string> vec = {
        "Hello", "from", "GCC", __VERSION__, "!" 
    };
    std::cout << vec << std::endl;
}

Sure you could gather up the

#include <iostream>
#include <vector>

in a separate header file, but that would be tedious to keep in sync of what you need in particular with all of your translation units.


Another related Q&A:

Why should I not #include <bits/stdc++.h>?

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
  • While I agree with your statement in general, I don't find it useful for teaching purposes, since somebody new to C++ does not know which standard headers to use. This is also one aspect I do not like in the book, not using the standard headers. Nevertheless, the anser by @Gilberto Albino is more suitable for learning C++ – ooxi Aug 21 '19 at 12:42
  • @ooxi The rule is pretty simple. You use the standard headers where the standard classes are defined. The [reference documentation](https://en.cppreference.com/w/cpp) available puts that out pretty well. – πάντα ῥεῖ Aug 21 '19 at 12:46
  • please keep in mind that this book is targeted at absolute beginners who might have never programmed in their life before. The part of the book referencing `std_lib_facilities.h` did not yet mention classes or the standard library, thus having students chase the correct header using the reference is much too early – ooxi Aug 26 '19 at 15:31
2

This is how I got C++11 to 17 working on Mac with Xcode installed. I am following the textbook 'Programming: Principles and Practice Using C++' by Bjarne Stroustrup. The following allows you to follow the textbook as Mr C++ himself wants you to. This works perfectly for me now with std_lib_facilities.h on Xcode and terminal. This is just what I did to get it working and the troubleshooting involved which can hopefully save you some time.

This is my .cpp file

// This program outputs the message "Hello, World!"

#include "std_lib_facilities.h"

int main()  // C++ programs start by executing the function main
{
    cout<<"Hello, World!\n";    // output "Hello, World!"
    keep_window_open();         //wait for a character to be entered
    return 0;
}

Stroustrup uses a custom header file called std_lib_facilities.h but for it to work I have had to make a few changes due to a couple errors that occured during compiling with the file currently on his website. It must be in the same folder as the .cpp if the code is like the above.

On Xcode you can change the C++ version in the project's build settings C++ language dialect

On Mac, I use clang++ instead of g++ to to compile files via terminal (these come with Xcode). To get terminal working with clang and g++ I had to enable developer tool permissions triggered by the command xcode-select --reset. You must specify the c++ version or you get a bunch of warnings e.g.

clang++ hello_world.cpp -std=c++14
./a.out

for the .h -> std:: was added before each ios_base and normal rather than curly brackets used for uniform_int_distribution

Replace your existing std_lib_facilities.h file with this:

/*
   std_lib_facilities.h
*/

/*
    simple "Programming: Principles and Practice using C++ (second edition)" course header to
    be used for the first few weeks.
    It provides the most common standard headers (in the global namespace)
    and minimal exception/error support.

    Students: please don't try to understand the details of headers just yet.
    All will be explained. This header is primarily used so that you don't have
    to understand every concept all at once.

    By Chapter 10, you don't need this file and after Chapter 21, you'll understand it

    Revised April 25, 2010: simple_error() added

    Revised November 25 2013: remove support for pre-C++11 compilers, use C++11: <chrono>
    Revised November 28 2013: add a few container algorithms
    Revised June 8 2014: added #ifndef to workaround Microsoft C++11 weakness
    Revised Febrary 2 2015: randint() can now be seeded (see exercise 5.13).
    Revised June 15 for defaultfloat hack for older GCCs
*/

#ifndef H112
#define H112 020215L


#include<iostream>
#include<iomanip>
#include<fstream>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<string>
#include<list>
#include <forward_list>
#include<vector>
#include<unordered_map>
#include<algorithm>
#include <array>
#include <regex>
#include<random>
#include<stdexcept>

//------------------------------------------------------------------------------
#if __GNUC__ && __GNUC__ < 5
inline std::ios_base& defaultfloat(std::ios_base& b)    // to augment fixed and scientific as in C++11
{
    b.setf(std::ios_base::fmtflags(0), std::ios_base::floatfield);
    return b;
}
#endif
//------------------------------------------------------------------------------

using Unicode = long;

//------------------------------------------------------------------------------

using namespace std;

template<class T> string to_string(const T& t)
{
    ostringstream os;
    os << t;
    return os.str();
}

struct Range_error : out_of_range { // enhanced vector range error reporting
    int index;
    Range_error(int i) :out_of_range("Range error: "+to_string(i)), index(i) { }
};


// trivially range-checked vector (no iterator checking):
template< class T> struct Vector : public std::vector<T> {
    using size_type = typename std::vector<T>::size_type;

#ifdef _MSC_VER
    // microsoft doesn't yet support C++11 inheriting constructors
    Vector() { }
    explicit Vector(size_type n) :std::vector<T>(n) {}
    Vector(size_type n, const T& v) :std::vector<T>(n,v) {}
    template <class I>
    Vector(I first, I last) : std::vector<T>(first, last) {}
    Vector(initializer_list<T> list) : std::vector<T>(list) {}
#else
    using std::vector<T>::vector;   // inheriting constructor
#endif

    T& operator[](unsigned int i) // rather than return at(i);
    {
        if (i<0||this->size()<=i) throw Range_error(i);
        return std::vector<T>::operator[](i);
    }
    const T& operator[](unsigned int i) const
    {
        if (i<0||this->size()<=i) throw Range_error(i);
        return std::vector<T>::operator[](i);
    }
};

// disgusting macro hack to get a range checked vector:
#define vector Vector

// trivially range-checked string (no iterator checking):
struct String : std::string {
    using size_type = std::string::size_type;
//  using string::string;

    char& operator[](unsigned int i) // rather than return at(i);
    {
        if (i<0||size()<=i) throw Range_error(i);
        return std::string::operator[](i);
    }

    const char& operator[](unsigned int i) const
    {
        if (i<0||size()<=i) throw Range_error(i);
        return std::string::operator[](i);
    }
};


namespace std {

    template<> struct hash<String>
    {
        size_t operator()(const String& s) const
        {
            return hash<std::string>()(s);
        }
    };

} // of namespace std


struct Exit : runtime_error {
    Exit(): runtime_error("Exit") {}
};

// error() simply disguises throws:
inline void error(const string& s)
{
    throw runtime_error(s);
}

inline void error(const string& s, const string& s2)
{
    error(s+s2);
}

inline void error(const string& s, int i)
{
    ostringstream os;
    os << s <<": " << i;
    error(os.str());
}


template<class T> char* as_bytes(T& i)  // needed for binary I/O
{
    void* addr = &i;    // get the address of the first byte
                        // of memory used to store the object
    return static_cast<char*>(addr); // treat that memory as bytes
}


inline void keep_window_open()
{
    cin.clear();
    cout << "Please enter a character to exit\n";
    char ch;
    cin >> ch;
    return;
}

inline void keep_window_open(string s)
{
    if (s=="") return;
    cin.clear();
    cin.ignore(120,'\n');
    for (;;) {
        cout << "Please enter " << s << " to exit\n";
        string ss;
        while (cin >> ss && ss!=s)
            cout << "Please enter " << s << " to exit\n";
        return;
    }
}



// error function to be used (only) until error() is introduced in Chapter 5:
inline void simple_error(string s)  // write ``error: s and exit program
{
    cerr << "error: " << s << '\n';
    keep_window_open();     // for some Windows environments
    exit(1);
}

// make std::min() and std::max() accessible on systems with antisocial macros:
#undef min
#undef max


// run-time checked narrowing cast (type conversion). See ???.
template<class R, class A> R narrow_cast(const A& a)
{
    R r = R(a);
    if (A(r)!=a) error(string("info loss"));
    return r;
}

// random number generators. See 24.7.

default_random_engine& get_rand()
{
    static default_random_engine ran;
    return ran;
};

void seed_randint(int s) { get_rand().seed(s); }

inline int randint(int min, int max) {  return uniform_int_distribution<>(min, max)(get_rand()); }

inline int randint(int max) { return randint(0, max); }

//inline double sqrt(int x) { return sqrt(double(x)); } // to match C++0x

// container algorithms. See 21.9.

template<typename C>
using Value_type = typename C::value_type;

template<typename C>
using Iterator = typename C::iterator;

template<typename C>
    // requires Container<C>()
void sort(C& c)
{
    std::sort(c.begin(), c.end());
}

template<typename C, typename Pred>
// requires Container<C>() && Binary_Predicate<Value_type<C>>()
void sort(C& c, Pred p)
{
    std::sort(c.begin(), c.end(), p);
}

template<typename C, typename Val>
    // requires Container<C>() && Equality_comparable<C,Val>()
Iterator<C> find(C& c, Val v)
{
    return std::find(c.begin(), c.end(), v);
}

template<typename C, typename Pred>
// requires Container<C>() && Predicate<Pred,Value_type<C>>()
Iterator<C> find_if(C& c, Pred p)
{
    return std::find_if(c.begin(), c.end(), p);
}

#endif //H112
2

Actually std_lib_facilities.h is created by Bjarne Stroustrup to help people who are very beginners in programming. You can also find it from his website: std_lib_facilities.h So this header file may not exist in your system. Bjarne Stroustrup also knows you may get error so he tells in his book to replace it with:-

//these header files are equivalent to std_lib_facilities.h 
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>

using namespace std; 

Hope this helps(^人^)

hddmss
  • 231
  • 1
  • 12
Ash_ish
  • 21
  • 1
0

I haven't read that book you referenced, but you can try switching to <iostream> and your program will compile without any errors.

I am guessing "std_lib_facilities.h" is more of a place holder than a realistic header.

Tanveer Badar
  • 5,438
  • 2
  • 27
  • 32
0

Using the updated header is probably the best solution. For those interested in this particular error: it seems to be caused by

// disgusting macro hack to get a range checked string:
#define string String

When <iomanip> is included after this macro, gcc runs into errors. When it is included before, gcc compiles fine. (Note: this macro is enabled only when _MSC_VER<1500; according to comments "MS C++ 9.0" does not get this macro.)

This is an object lesson on why hacks are not recommended. They break over time.

JaMiT
  • 14,422
  • 4
  • 15
  • 31