繁体   English   中英

创建内存分配器时出现C ++继承错误

[英]C++ inheritance error when creating memory allocators

我想知道为什么以下代码不起作用。 其背后的主要思想如下:

  1. 我想使用分别由std分配器和boost :: interprocess :: allocator继承的自定义类,并且我想使用它们而不是基本分配器
  2. 当我创建一个MyStdAllocator类时,该类由std :: allocator继承并使用它代替std :: allocator(请参阅变量x1和x2),它可以正常工作,并且不发出警告,也没有错误。
  3. 当我创建一个MyBoostAllocator类时,该类由boost :: interprocess :: allocator继承并使用它代替boost :: interprocess:allocator(请参见变量y1和y2),它无法正常工作,并且在此底部列出了编译错误题。

您能否告诉我,为什么这种继承无效,以及如何解决呢?

#include <boost/interprocess/managed_shared_memory.hpp>
#include <boost/interprocess/allocators/allocator.hpp>
#include <iostream>

using namespace boost::interprocess;


template <typename _Tp>
class MyStdAllocator : public std::allocator<_Tp>
{
};


template<class T, class SegmentManager>
class MyBoostAllocator : public allocator<T, SegmentManager>
{
};



typedef std::allocator<int> std_allocator;
typedef MyStdAllocator<int> my_std_allocator;

typedef allocator       <int, managed_shared_memory::segment_manager> boost_allocator;
typedef MyBoostAllocator<int, managed_shared_memory::segment_manager> my_boost_allocator;


int main(int argc, char** argv) {

    struct shm_remove {
        shm_remove() {
            shared_memory_object::remove("MySharedMemory");
        }

        ~shm_remove() {
            shared_memory_object::remove("MySharedMemory");
        }
    } remover;

    managed_shared_memory segment(create_only,
            "MySharedMemory", //segment name
            65536);

    //Create an allocator that allocates ints from the managed segment
    allocator<int, managed_shared_memory::segment_manager>
            allocator_instance(segment.get_segment_manager());


    std_allocator *x1;
    x1 = new std_allocator();
    delete x1;

    my_std_allocator *x2;
    x2 = new my_std_allocator();
    delete x2;


    boost_allocator *y1;
    y1 = new boost_allocator(segment.get_segment_manager());
    delete y1;

    // following lines generate compilation errors:
    my_boost_allocator *y2;
    y2 = new my_boost_allocator(segment.get_segment_manager());
    delete y2;

    std::cout<<"its working!\n";

    return 0;

}

编译错误如下:

../src/test.cpp:73:59: error: no matching function for call to ‘MyBoostAllocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::MyBoostAllocator(boost::interprocess::ipcdetail::basic_managed_memory_impl<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index, 16ul>::segment_manager*)’
../src/test.cpp:73:59: note: candidates are:
../src/test.cpp:24:7: note: MyBoostAllocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::MyBoostAllocator()
../src/test.cpp:24:7: note:   candidate expects 0 arguments, 1 provided
../src/test.cpp:24:7: note: MyBoostAllocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >::MyBoostAllocator(const MyBoostAllocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >&)
../src/test.cpp:24:7: note:   no known conversion for argument 1 from ‘boost::interprocess::ipcdetail::basic_managed_memory_impl<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index, 16ul>::segment_manager* {aka boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index>*}’ to ‘const MyBoostAllocator<int, boost::interprocess::segment_manager<char, boost::interprocess::rbtree_best_fit<boost::interprocess::mutex_family>, boost::interprocess::iset_index> >&’
make: *** [src/test.o] Error 1

这似乎是一个“受支持的C ++ 11”问题。 看到

http://wiki.apache.org/stdcxx/C++0xCompilerSupport

哪些编译器支持哪些功能。

继承构造函数仅适用于gnu4.8 +。

当它们仍然存在时,您仍然必须显式地放入using构造。

template<class T, class SegmentManager> 
class MyBoostAllocator : public allocator<T, SegmentManager> 
{ 
 public:
     using allocator<T, SegmentManager >::allocator;
};  

在可用之前,在派生类中编写构造函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM