문제

I'm using the STL allocator mentioned here.
The only change I'm making is that I'm inheriting from a base class called Object, and I use base class' new and delete functions for allocation.

    class MyAlloc :public Object{
    ......
    }

I want to use the parameterized constructor of the base class which will be based on parameter sent to the STLAllocator, which would be something like this.

    MyAlloc(A *a) : Object(a) {
    ... }

And then use this constructor like :

   A *a = new A();
   std::vector<int,MyAlloc<int> (a) > v;

I'm not able to achieve this. It is resulting in compilation error :
'a'cannot appear in a constant-expression
template argument 2 is invalid
Thanks in advance..:)

도움이 되었습니까?

해결책

You specify the type of the allocator as a template argument and, if you don't want a default-constructed one, a value as a constructor argument:

std::vector<int,MyAlloc<int>> v((MyAlloc<int>(a)));

Note that I added an extra pair of parentheses to avoid the "most vexing parse". In this case, we can't avoid that using brace-initialisation, since that will try to use the initialiser list to populate the vector.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top