Bug 56613 - [4.8 Regression] map::operator[](key_type&&) fails with custom allocator
Summary: [4.8 Regression] map::operator[](key_type&&) fails with custom allocator
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.8.0
: P1 normal
Target Milestone: 4.8.0
Assignee: Jonathan Wakely
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2013-03-13 13:32 UTC by Jonathan Wakely
Modified: 2013-03-14 23:30 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work: 4.7.2
Known to fail:
Last reconfirmed: 2013-03-14 00:00:00


Attachments
fix the bug by using allocator_traits (1.49 KB, patch)
2013-03-14 01:43 UTC, Jonathan Wakely
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Jonathan Wakely 2013-03-13 13:32:27 UTC
This program is valid in C++03 and C++11 but can't be compiled with trunk using -std=c++11

#include <map>

template<typename T>
struct alloc
{
    typedef T value_type;
    typedef T* pointer;
    typedef const T* const_pointer;
    typedef T& reference;
    typedef const T& const_reference;
    typedef unsigned size_type;
    typedef int difference_type;

    template<typename U>
        struct rebind {
            typedef alloc<U> other;
        };

    alloc() { }
    template<typename U>
        alloc(const alloc<U>&) { }

    pointer allocate(size_type n, const void* = 0) { return std::allocator<T>().allocate(n); }
    void deallocate(pointer p, size_type n) { std::allocator<T>().deallocate(p, n); }

    size_type max_size() const { return -1; }

    void construct(pointer p, const T& t) { new ((void*) p) T(t); }
    void destroy(pointer p) { p->~T(); }
};

template<typename T, typename U>
bool operator==(alloc<T>, alloc<U>) { return true; }

template<typename T, typename U>
bool operator!=(alloc<T>, alloc<U>) { return false; }

int main()
{
    std::map<int, int, std::less<int>, alloc<int> > m;
    m[1];
}


$ g++-4.8 -std=c++11 map.cc
In file included from /home/redi/gcc/4.x/include/c++/4.8.0/map:60:0,
                 from map.cc:1:
/home/redi/gcc/4.x/include/c++/4.8.0/bits/stl_tree.h: In instantiation of 'std::_Rb_tree_node<_Val>* std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_create_node(_Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, int>; _KeyOfValue = std::_Select1st<std::pair<const int, int> >; _Compare = std::less<int>; _Alloc = alloc<std::pair<const int, int> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Link_type = std::_Rb_tree_node<std::pair<const int, int> >*]':
/home/redi/gcc/4.x/include/c++/4.8.0/bits/stl_tree.h:1662:64:   required from 'std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_emplace_hint_unique(std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator, _Args&& ...) [with _Args = {const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>}; _Key = int; _Val = std::pair<const int, int>; _KeyOfValue = std::_Select1st<std::pair<const int, int> >; _Compare = std::less<int>; _Alloc = alloc<std::pair<const int, int> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const int, int> >; std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const int, int> >]'
/home/redi/gcc/4.x/include/c++/4.8.0/bits/stl_map.h:484:8:   required from 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = int; _Tp = int; _Compare = std::less<int>; _Alloc = alloc<int>; std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type = int; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = int]'
map.cc:41:8:   required from here
/home/redi/gcc/4.x/include/c++/4.8.0/bits/stl_tree.h:403:8: error: no matching function for call to 'alloc<std::_Rb_tree_node<std::pair<const int, int> > >::construct(std::_Rb_tree_node<std::pair<const int, int> >*&, const std::piecewise_construct_t&, std::tuple<int&&>, std::tuple<>)'
        _M_get_Node_allocator().construct(__tmp,
        ^
/home/redi/gcc/4.x/include/c++/4.8.0/bits/stl_tree.h:403:8: note: candidate is:
map.cc:28:10: note: void alloc<T>::construct(alloc<T>::pointer, const T&) [with T = std::_Rb_tree_node<std::pair<const int, int> >; alloc<T>::pointer = std::_Rb_tree_node<std::pair<const int, int> >*]
     void construct(pointer p, const T& t) { new ((void*) p) T(t); }
          ^
map.cc:28:10: note:   candidate expects 2 arguments, 4 provided


The problem is that stl_map.h uses piecewise construction for the pair object, which calls alloc::construct with four arguments, incorrectly assuming that all allocators in C++11 have a variadic construct() member.

It's easily fixed by changing the C++11 version of _M_create_node to use allocator_traits::construct:

                std::allocator_traits<_Node_allocator>::
                    construct(_M_get_Node_allocator(), __tmp,
                              std::forward<_Args>(__args)...);


(For 4.9 I was going to make all the RB-trees allocator-aware, but this regression needs to be fixed for 4.8 without the rest of the allocator changes)
Comment 1 Jonathan Wakely 2013-03-13 13:35:14 UTC
Oops, the program isn't quite valid C++03, I forgot these members of the allocator:

  pointer       address(reference x) const throw()       { return &x; }
  const_pointer address(const_reference x) const throw() { return &x; }

That doesn't affect the issue though.
Comment 2 Jonathan Wakely 2013-03-14 01:43:53 UTC
Created attachment 29664 [details]
fix the bug by using allocator_traits
Comment 3 Jonathan Wakely 2013-03-14 23:28:18 UTC
Author: redi
Date: Thu Mar 14 23:28:11 2013
New Revision: 196666

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=196666
Log:
	PR libstdc++/56613
	* include/bits/stl_tree.h (_Rb_tree::_M_create_node): Use
	allocator_traits instead of calling construct directly.
	* testsuite/23_containers/map/56613.cc: New.

Added:
    trunk/libstdc++-v3/testsuite/23_containers/map/56613.cc
Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/include/bits/stl_tree.h
Comment 4 Jonathan Wakely 2013-03-14 23:30:53 UTC
fixed