开发者

Const incorrectness

Why am I getting this error:

Error 1 error C2662: 'Allocator::Allocate' : cannot convert 'this' pointer from 'const Allocator' to 'Allocator &' ?

Thats code:

/*Allocator.h*/
    /*Not finished yet but working*/
    #pragma once
    template<class T>
    class Allocator
    {
    public:
        //typedef T value_type;
        typedef T* pointer;
        pointer Allocate(std::size_t count);
        pointer Construct(void* address, const pointer obj);
        template<class FwdIter>
        void Destroy_(FwdIter first,FwdIter last);
        void Deallocate_(void* where);
        Allocator();
        ~Allocator();

    private:
        void Destroy_(const T* obj);


    };
/*Allocator_impl.hpp*/
#pragma once
#include "StdAfx.h"
#include "Allocator.h"

template<class T>
Allocator<T>::Allocator()
{
}


template<class T>
Allocator<T>::~Allocator()
{
    /*Destroy();
    Deallocate();*/
}

template<class T>
typename Allocator<T>::pointer Allocator<T>::Allocate(std::size_t count)
{
    return static_cast<pointer>(::operator new(sizeof(value_type) * count));
}

template<class T>
typename Allocator<T>::pointer Allocator<T>::Construct(void* address, const pointer obj)
{
    return new (address) T(*obj);
}

//template<class T>
//void Allocator<T>::Destroy()
//{
//  //Destroy_(addressBegin_, addressBegin_ + size_);
//}

template<class T>
void Allocator<T>::Destroy_(const T* obj)
{
    obj->~T();
}

template<class T>
template<class FwdIter>
void Allocator<T>::Destroy_(FwdIter first,FwdIter last)
{
    while (first != last)
    {
        Destroy_(&*first);
        ++first;
    }
}

template<class T>
void Allocator<T>::Deallocate_(void* address)
{
    ::operator delete(address);
}

//template<class T>
//void Allocator<T>::Deallocate()
//{
//  //Deallocate_(addressBegin_);
//}
/*Toy.h*/
        #pragma once
    #include "Allocator_impl.hpp"


    /*As a base to managed memory*/
    template<class T, class A = Allocator<T>>
    class ToyBase
    {   
        typedef T* pointer;
    private:
        A alloc_;
    protected:
//--------------------------------------COMMENT HERE
            pointer Allocate(const std::size_t)const;<------------When invoking this fnc from
        explicit ToyBase();
        virtual ~ToyBase();
    };

    template<class T, class A>
    ToyBase<T,A>::ToyBase()
    {}


    template<class T, class A>
    ToyBase<T,A>::~ToyBase()
    {}
    //--------------------------------------AND COMMENT HERE
    template<class T, class A>
    typename ToyBase<T,A>::pointer ToyBase<T,A>::Allocate(const std::size_t count)const
    {
        return alloc_.Allocate(count);<-----------here
    }
    /*
But when I remove const from fnc decl. it works. I do not understand it as I do not change an object merely invoke fnc on its member.
*/



    template<class T>
    class ToyRepresentation : private ToyBase<T>
    {
    public:
        typedef T value_type;
        typedef T* pointer;
        ToyRepresentation(const std::size_t = 0);
        void Push(T*);
        void Pop();
        void GetSize()const;
        void GetCapacity()const;
        void SetCapacity(const std::size_t);
        void Reset();
    private:
        pointer data_;
        std::size_t size_;
        std::size_t capacity_;
        static unsigned TOTAL_; //total number of created objects
    };

    template<class T>
    unsigned ToyRepresentation<T>::TOTAL_ = 0;


    template<class T>
    ToyRepresentation<T>::ToyRepresentation(const std::size_t count = 0): ToyBase<T>(), data_(Allocate(count)), size_(0), capacity_(count)
    {
    }

    /*tmain*/ 
#include "stdafx.h"
#include "Toy.h" 
int _tmain(int argc, _TCHAR* argv[])
{
    try
    {
        ToyRepresentation<int> t;
    }
    catc开发者_开发问答h(const std::exception&)
    {
    }
    return 0;
}

Comments to interesting lines are marked in code. Thanks.


alloc_.Allocate is not a const method. You can "fix" (or hide) this warning by making alloc_ mutable, although this should not be done without understanding of why the compiler is warning you.

Not sure why the method that calls this needs to be const anyhow. Allocation is not something that typically is expected to leave its context unchanged.


ToyBase<T,A>::Allocate is const qualified which means that you cannot invoke any non-const methods on any members of this as they too are const-qualified now.

Try FAQ 18.10.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜