Chapter 5. Typename

Difficulty: 7

“What's in a (type) name?” Here's an exercise that demonstrates why and how to use typename, using an idiom that's common in the standard library.

  1. What is typename, and what does it do?

  2. What, if anything, is wrong with the code below?

    template<typename T>
    class X_base
    {
    public:
      typedef T instantiated_type;
    };
    
    template<typename A, typename B>
    class X : public X_base<B>
    {
    public:
      bool operator()( const instantiated_type& i ) const
      {
        return i != instantiated_type();
      }
    
      // ... more stuff ...
    };
    

Solution

Solution
  • 1. What is typename, and what does it do?

This gets us back into the field of name lookup. A motivating example is dependent ...

Get More Exceptional C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.