Dav*_*Woo 9 c++ c++11 std-function
我正在使用std :: function和自定义分配器,但是当我没有为初始仿函数提供函数时,它的行为不像我预期的那样.
当我向构造函数提供自定义分配器但没有初始函子时,分配器从未使用过或者看起来如此.
这是我的代码.
//Simple functor class that is big to force allocations
struct Functor128
{
Functor128()
{}
char someBytes[128];
void operator()(int something)
{
cout << "Functor128 Called with value " << something << endl;
}
};
int main(int argc, char* argv[])
{
Allocator<char, 1> myAllocator1;
Allocator<char, 2> myAllocator2;
Allocator<char, 3> myAllocator3;
Functor128 myFunctor;
cout << "setting up function1" << endl;
function<void(int)> myFunction1(allocator_arg, myAllocator1, myFunctor);
myFunction1(7);
cout << "setting up function2" << endl;
function<void(int)> myFunction2(allocator_arg, myAllocator2);
myFunction2 = myFunctor;
myFunction2(9);
cout << "setting up function3" << endl;
function<void(int)> myFunction3(allocator_arg, myAllocator3);
myFunction3 = myFunction1;
myFunction3(19);
}
Run Code Online (Sandbox Code Playgroud)
输出:
setting up function1
Allocator 1 allocating 136 bytes.
Functor128 Called with value 7
setting up function2
Functor128 Called with value 9
setting up function3
Allocator 1 allocating 136 bytes.
Functor128 Called with value 19
Run Code Online (Sandbox Code Playgroud)
所以case1:myFunction1按预期使用allocator1进行分配.
case2:myFunction2在构造函数中被赋予allocator2,但是当它被赋予一个仿函数时,它似乎重置为使用默认的std :: allocator来进行分配.(因此没有关于分配的打印).
case3:myFunction3在构造函数中被赋予allocator3但是当从myFunction1赋值时,使用function1的allocator进行分配以进行分配.
这是正确的行为吗?特别是,在案例2中为什么要恢复使用默认的std :: allocator?如果是这样,那么采用分配器的空构造函数的重点是什么,因为分配器永远不会被使用.
我正在使用VS2013代码.
我的Allocator类只是一个最小的实现,它使用new并在分配时注销
template<typename T, int id = 1>
class Allocator {
public:
// typedefs
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
public:
// convert an allocator<T> to allocator<U>
template<typename U>
struct rebind {
typedef Allocator<U> other;
};
public:
inline Allocator() {}
inline ~Allocator() {}
inline Allocator(Allocator const&) {}
template<typename U>
inline Allocator(Allocator<U> const&) {}
// address
inline pointer address(reference r) { return &r; }
inline const_pointer address(const_reference r) { return &r; }
// memory allocation
inline pointer allocate(size_type cnt,
typename std::allocator<void>::const_pointer = 0)
{
size_t numBytes = cnt * sizeof (T);
std::cout << "Allocator " << id << " allocating " << numBytes << " bytes." << std::endl;
return reinterpret_cast<pointer>(::operator new(numBytes));
}
inline void deallocate(pointer p, size_type) {
::operator delete(p);
}
// size
inline size_type max_size() const {
return std::numeric_limits<size_type>::max() / sizeof(T);
}
// construction/destruction
inline void construct(pointer p, const T& t) { new(p)T(t); }
inline void destroy(pointer p) { p->~T(); }
inline bool operator==(Allocator const&) { return true; }
inline bool operator!=(Allocator const& a) { return !operator==(a); }
}; // end of class Allocator
Run Code Online (Sandbox Code Playgroud)
std::function分配器支持是......很奇怪.
目前的规格operator=(F&& f)是它确实如此std::function(std::forward<F>(f)).swap(*this);.如您所见,这意味着默认情况下f使用任何std::function使用来分配内存,而不是用于构造的分配器*this.所以你观察到的行为是正确的,尽管令人惊讶.
此外,由于(allocator_arg_t, Allocator)和(allocator_arg_t, Allocator, nullptr_t)构造函数是noexcept,它们甚至不能真正存储分配器,即使它们想要(类型擦除分配器可能需要动态分配).因此,它们基本上是无操作,以支持使用分配器构造协议.
LWG最近拒绝了一个会改变这种行为的问题.
| 归档时间: |
|
| 查看次数: |
764 次 |
| 最近记录: |