1

I have been modifying a class which contains an STL container into a template so that an allocator can be passed in.

This goes as follows:

class Foo
{
public:
    struct Bar
    { 
     // stuff
    };

    using Container = std::vector< Bar >;
private:
    Container contents;
};

Becomes:

template<
  template <typename T> typename ALLOCATOR = std::allocator
  >
class Foo
{
public:
    struct Bar
    { 
     // stuff
    };

    using Allocator = ALLOCATOR<Bar>;

    using Container = std::vector< Bar, Allocator >;
private:
    Container contents;
};

This works great on all of the platforms I have to support except one. RHEL7 uses gcc 4.8 by default and use of typename in template type parameters seems to have been added by n4051 in gcc 5 according to this table. How can this be done for older compilers?

Also I note this is a C++17 feature. How would these parameters be written in C++14 or earlier?

Bruce Adams
  • 4,953
  • 4
  • 48
  • 111

1 Answers1

3

The answer is in fact very simple. Just replace typename with class. That is, instead of writing this:

template<
  template <typename T> typename ALLOCATOR = std::allocator
>

write this instead:

template<
  template <class T> class ALLOCATOR = std::allocator
>

As noted in n4051, which you quote:

This difference is artificial and is a common surprise.

There is no semantic difference between class and typename in a template-parameter type-parameter-key

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Bruce Adams
  • 4,953
  • 4
  • 48
  • 111