仅使用静态方法指向 class 的指针

Pointer to class with only static methods

有没有办法获得指向仅由静态方法组成的 class 的指针?没有成员变量。

我有一个向量 class,使用 typename alloc = allocator<T> 作为模板参数之一。 allocator<T> 模板 class 完全由静态方法组成。我打算实现一个 get_allocator 方法,该方法提供对给定向量分配器方法的访问。例如。这样就可以了:

int main() {
    custom::vector<int> my_vector(10); // custom int-vector of initial size and capacity 10
    
    // use my_vector's allocator<int> instead of new (don't ask why)
    int* my_array = my_vector.get_allocator()->allocate(5); 
    // ^^ rhs should produce same result as new int[5]. allocator<T>::allocate uses ::operator new.
}

里面custom::vector我有这个方法:

alloc* get_allocator() {
    return &alloc
}

这将引发 MSVC 编译器错误 2275:'alloc':非法使用此类型作为表达式 在签名前加上前缀 typename 没有帮助。

分配器 class 看起来像这样:

template <typename T>
class allocator : public mem_core::base_allocator<T> {
public:

    using value_type = T;
    using T_ptr = T*;
    using T_ref = T&;

    static T_ptr address(T_ref value) noexcept {
        return mem_core::addressof<T>(value);
    }

    static void deallocate(T_ptr const ptr, const size_t& count) {
        mem_core::deallocate<T>(ptr, count);
    }

    static T_ptr allocate(const size_t& amount) {
        return mem_core::allocate<T>(amount);
    }
private:
    allocator() = default;
};

你不能得到一个指向类型的指针(例如&int),只能得到一个类型的对象。所以要么创建一个对象和 return 指向它的指针,要么直接通过类型使用静态函数:

custom::vector<int>::alloc::allocate(5).