c++definitionstatic-memberstemplate-templates

static member definition outside class template template


I'm getting:

error: default argument for template parameter for class enclosing 'ticker<T, E, A>::garbage_element'
   51 | E ticker<T,E,A> ::garbage_element;
      |                   ^~~~~~~~~~~~~~~

l know if I use the keyword "inline" like this:

    inline static  E garbage_element;

inside the "ticker" template, it compiles fine, but how in the world it suppose to look like outside the template.

#include <iostream>
#include <vector>

template< template<class, class> class T, class E, class A = std::allocator<E>  >
class ticker
{
    T<E,A>* container;
    int current_index;
    bool mode;
    static  E garbage_element;
 public:
    // constructors and members fn
};

template< template<class, class> class T, class E, class A = std::allocator<E> >
E ticker<T,E,A> ::garbage_element; 


Solution

  • When defining the static data member of a class template which has a default argument for one of its template parameter, the default argument should not be repeated. It is needed only once when the class template is first declared/defined.

    This means you don't need to specify the default argument for parameter A when defining the member garbage_element outside the class as shown below:

    //-------------------------------------------------------v-->no default argument here
    template<template<class, class> class T, class E, class A>
    E ticker<T,E,A>::garbage_element;
    

    Working demo