Opened 10 years ago

Closed 9 years ago

#8407 closed Bugs (fixed)

Unclear docs for is_base_of

Reported by: driscoll@… Owned by: John Maddock
Milestone: To Be Determined Component: type_traits
Version: Boost 1.53.0 Severity: Problem
Keywords: Cc:

Description

It isn't clear (at least to me) from the documentation for is_base_of whether it evaluates to true_type when Base is an direct base class of Derived or any inheritance ancestor. It would be nice if this were clarified.

(I understand the first para of chapter 10 of the C++03 standard to define the term "base class" as including both direct and indirect base classes, but IMO this is surprising; myself and two nearby people I informally polled equate "base class" with "direct base class". At the suggestion of a fourth, we looked it up in the standard for the purposes of this report.)

I had to try the following program to figure out what the behavior is (which seems to be "true if Base is any ancestor of Derived"):

#include <boost/type_traits.hpp>
#include <iostream>

struct Base {};
struct Intermediate : Base {};
struct Derived : Intermediate {};

template <typename Base, typename Derived>
const char * answer(Base a, Derived b) {
    return boost::is_base_of<Base, Derived>::value ? "yes" : "no";
}

int main() {
    Base b;
    Intermediate i;
    Derived d;   
    std::cout << "Checks:\n"
              << "  Base is a base class of base [expect yes]:      " << answer(b, b) << "\n"
              << "  Inter. is a base class of base [expect yes]:    " << answer(b, i) << "\n"
              << "  Derived is a base class of inter. [expect yes]: " << answer(i, d) << "\n"
              << "  Base is a base class of derived [expect no]:    " << answer(d, b) << "\n"
              << "Actual answer:\n"
              << "  Derived is a base class of base: " << answer(b, d) << "\n";
}

Change History (2)

comment:1 by John Maddock, 9 years ago

(In [84761]) Clarify that base classes include indirect ancestors. Refs #8407.

comment:2 by John Maddock, 9 years ago

Resolution: fixed
Status: newclosed

(In [84762]) Clarify that base classes include indirect ancestors. Fixes #8407.

Note: See TracTickets for help on using tickets.