2

次のコードが機能しないのはなぜだろうか。その背後にある主なアイデアは次のとおりです。

  1. stdアロケータとboost::interprocess :: allocatorをそれぞれ継承するカスタムクラスを使用したいのですが、ベースアロケータの代わりにそれらを使用したいと思います。
  2. std :: allocatorを継承し、std :: allocatorの代わりに使用するクラスMyStdAllocatorを作成すると(変数x1およびx2を参照)、機能し、警告もエラーも発生しません。
  3. boost :: interprocess :: allocatorを継承し、boost :: interprocess:allocatorの代わりに使用するクラスMyBoostAllocatorを作成すると(変数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
4

1 に答える 1

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;
};  

それが利用可能になるまで、派生クラスにコンストラクターを記述します。

于 2012-10-24T15:04:06.993 に答える