What is Negative_size exception in Bjarne’s book?

2nd edition of A Tour of C++ of Bjarne’s Stroustrup goes into templates in chapter 6.

This is the code on page 80 that I am uncertain about. There is a Negative_size exception but I haven’t been able to find it with a google search and adding std:: to it gave me the error:

error: ‘Negative_size’ is not a member of ‘std’
   26 |         throw std::Negative_size{};
template <typename T>
class Vector
{
public:
    explicit Vector(int s);
    ~Vector()
    {
        delete[] elem;
    }

    T &operator[](int i);
    const T &operator[](int i) const; // for const Vectors
    int size() const
    {
        return sz;
    }

private:
    T *elem;
    int sz;
};

template <typename T>
Vector<T>::Vector(int s)
{

    if (s < 0)
    {
        throw std::Negative_size{}; // where the compilation error occurs
    }
}

I also didn’t see it mentioned in the errata.

EDIT:

Responses to comments

  • Chapter 6 is about templates.
  • I have read the book from the first chapter and so I haven’t skipped anything. I wouldn’t have posted a question otherwise.
  • I initially posted code to figure out what the error is but I am including the rest to reproduce the problem. I didn’t want to include extra code that wasn’t asking about the issue itself but this was requested.

  • Please post a minimal reproducible example so we could see that error. The shown code raises other errors, starting from the undefined identifier Vector.

    – 

  • 3

    There is no standard Negative_size type. From context, I assume it is a hypothetical type used for illustrative purposes. For the purpose of illustrating this example, it doesn’t matter how Negative_size is implemented.

    – 

  • 1

    Negative_size is not a standard type (and neither is Vector) in C++. Either Negative_size is described somehow (e.g. as pseudo-code) in some section of the book that you have skipped or it is a hypothetical type that may be inferred from context to be “an unspecified type thrown if a negative size is detected”.

    – 

  • Are you one of who is using namespace std;? Then I doubt it’s a book fault.

    – 

  • 1

    “adding std:: to it gave me the error” — Not too surprising. A capitalized identifier is unlikely to be in the std namespace. Most identifiers in the standard library are all lowercase, while a few are all caps (and the all caps examples I can think of are not in a namespace — they tend to be preprocessor macros).

    – 




Leave a Comment