title: Force static method in class based on interface in C++
body: <p>I am making a vector-class which has the option to take an allocator as a template parameter. To ensure a user-defined allocator works with the vector, I provide an interface which sets the minimum requirements for a given allocator. Since the allocator is just used to allocate and deallocate, it has no member variables, and all methods are static. Is there a way to make sure any implementation has a given set of methods that are also static? I know you cannot have a method be static and virtual at the same time, but I essentially want any allocator derived from base_allocator to have a set of methods, and for those methods to be static. <strong>In short: <strong>Can you work around the <code>virtual static</code> contradiction in c++?</strong></strong></p>
  <p>This is my current code:</p>
  <pre><code>// in namespace mem_core
  // Interface for usage of custom allocators with the custom:: namespace containers.
  template &lt;typename T&gt;
  class base_allocator {
  public:
      using T_ptr = T*;
      using T_ref = T&amp;;
  
      virtual T_ptr address(const T_ref value);
  
      // memory methods should allocate for amount elements of type T
      virtual void deallocate(T_ptr const ptr, const size_t&amp; count) = 0;
      virtual T_ptr allocate(const size_t&amp; amount) = 0;
  };
  
  _MEMORY_CORE_END_ // namespace mem_core }
  
  #undef _MEMORY_CORE_BEGIN_
  #undef _MEMORY_CORE_END_
  
  // default allocator CLASS TEMPLATE
  template &lt;typename T&gt;
  class allocator : public mem_core::base_allocator&lt;T&gt; {
  public:
  
      using value_type = T;
      using T_ptr = T*;
      using T_ref = T&amp;;
  
      static T_ptr address(T_ref value) noexcept {
          return mem_core::addressof&lt;T&gt;(value);
      }
  
      static void deallocate(T_ptr const ptr, const size_t&amp; count) {
          mem_core::deallocate&lt;T&gt;(ptr, count);
      }
  
      static T_ptr allocate(const size_t&amp; amount) {
          return mem_core::allocate&lt;T&gt;(amount);
      }
  };
  </code></pre>
  <p>This of course works, but there is no guarantee that a user-defined allocator has made the virtual methods static, as that is a requirement for the allocator to work within the vector-class.
  e.g. like this:</p>
  <pre><code>template &lt;typename T, typename alloc_type = allocator&lt;T&gt;&gt;
  class vector {
  public:
  
      using iterator = vector_iterator&lt;vector&lt;T, alloc_type&gt;&gt;;
  
      using T_ptr = T*;
      using T_ref = T&amp;;
  
      using value_type = T;
  private:
      T_ptr m_data;
      size_t m_size;
      size_t m_capacity;
  public:
      vector() : m_data(nullptr), m_size(0), m_capacity(0) noexcept {};
  
      vector(const size_t&amp; initial_cap) : m_size(0) {
          m_data = alloc_type::allocate(m_capacity); // Requires static allocate method
          m_capacity = initial_cap;
      }
  // rest of vector class ...
  </code></pre>