c++ - 模板化 ctor 上的编译器错误(这是编译器错误吗?)

标签 c++ visual-c++ compiler-errors visual-studio-2019

当我尝试从 this post 编译代码时(见下文)在 Visual Studio 2019 中,我在模板化复制 ctor 行收到以下编译器错误:

Error   C2439   'PreAllocator<_Newfirst>::memory_ptr': member could not be initialized
Error   C2440   'initializing': cannot convert from 'T *const ' to 'T *'
Error   C2248   'PreAllocator<int>::memory_size': cannot access private member declared in class 'PreAllocator<int>'    
Error   C2248   'PreAllocator<int>::memory_ptr': cannot access private member declared in class 'PreAllocator<int>' 
这是编译器错误还是我遗漏了什么?
template <typename T>
class PreAllocator
{
private:
    T* memory_ptr;
    std::size_t memory_size;

public:
    typedef std::size_t     size_type;
    typedef T* pointer;
    typedef T               value_type;

    PreAllocator(T* memory_ptr, std::size_t memory_size) : memory_ptr(memory_ptr), memory_size(memory_size) {}

    PreAllocator(const PreAllocator& other) throw() : memory_ptr(other.memory_ptr), memory_size(other.memory_size) {};

    template<typename U>
    PreAllocator(const PreAllocator<U>& other) throw() : memory_ptr(other.memory_ptr), memory_size(other.memory_size) {};

    template<typename U>
    PreAllocator& operator = (const PreAllocator<U>& other) { return *this; }
    PreAllocator<T>& operator = (const PreAllocator& other) { return *this; }
    ~PreAllocator() {}

    pointer allocate(size_type n, const void* hint = 0) { return memory_ptr; }
    void deallocate(T* ptr, size_type n) {}

    size_type max_size() const { return memory_size; }
};

int main()
{
    int my_arr[100] = { 0 };
    std::vector<int, PreAllocator<int>> my_vec(0, PreAllocator<int>(&my_arr[0], 100));
}

最佳答案

template<typename U>
PreAllocator(const PreAllocator<U>& other) throw() : memory_ptr(other.memory_ptr), memory_size(other.memory_size) {};
此构造函数尝试初始化 memory_ptr类型 T*来自 other.memory_ptr类型 U* , 不同于 T* (因为否则会调用复制构造函数)。 std::vector实现显然使用内部类型来实例化分配器来分配存储,并且指向该类型的指针不能隐式转换为int* ,你所期望的。
您必须显式转换指针,或存储 void*在内部,然后您将其转换为 T*allocate成员函数。
template <typename T>
class PreAllocator
{
private:
    void* memory_ptr;

    ...

public:
    template<typename U>
    PreAllocator(const PreAllocator<U>& other) throw() :
        memory_ptr(other.memory_ptr), memory_size(other.memory_size) {}

    pointer allocate(size_type n, const void* hint = 0)
    {
        return static_cast< pointer >(memory_ptr);
    }

    ...
};
此外,在同一个构造函数中,您正在访问 memory_ptrmemory_size PreAllocator<U> 的成员,这是与 PreAllocator<T> 不同且不相关的类型(同样,因为 TU 是不同的类型)。这些成员在该类中是私有(private)的,这会给您带来访问错误。
您可以通过将这些成员设为公共(public)或为它们添加公共(public)访问器,或通过使 PreAllocator 的所有特化来缓解这种情况。有这样声明的 friend :
template <typename T>
class PreAllocator
{
    template< typename U >
    friend class PreAllocator;

    ...
};

关于c++ - 模板化 ctor 上的编译器错误(这是编译器错误吗?),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65807982/

相关文章:

c++ - 两种链接 boost 正则表达式的方法出错

java - 我可以实例化一个开关吗?或者说这有什么问题吗?

java - 错误 : missing return statement despite there being a return statement

java - 无法执行目标org.apache.maven.plugins :maven-compiler-plugin:3.1:compile (default-compile) on project [duplicate]

c++ - 用于音频文件流的 FFMPEG 命令

c++ - Qt C++ 在 iOS 11.4.1 上隐藏键盘

c++ - 使用 cmake 构建可执行文件和共享库,runtimelinker 找不到 dll

c++ - 如何通过单击 MainFrame 中的按钮来更改 MFC View

c++ - 使用 C++ AMP 时未命中 GPU 断点

c++ - 命令提示符中的大输入