[llvm-bugs] [Bug 24933] New: std::basic_string requires Allocator to have a default constructor

via llvm-bugs llvm-bugs at lists.llvm.org
Thu Sep 24 21:02:45 PDT 2015


https://llvm.org/bugs/show_bug.cgi?id=24933

            Bug ID: 24933
           Summary: std::basic_string requires Allocator to have a default
                    constructor
           Product: libc++
           Version: 3.6
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P
         Component: All Bugs
          Assignee: unassignedclangbugs at nondot.org
          Reporter: jhall1024+llvmbugs at gmail.com
                CC: llvm-bugs at lists.llvm.org, mclow.lists at gmail.com
    Classification: Unclassified

Instantiating std::basic_string<char, std::char_traits<char>,
MyAllocator<char>> fails if MyAllocator doesn't have a default constructor.
Allocators are not supposed to require a default constructor, and with stateful
allocators in C++11 a default constructor doesn't make sense.

Tried with Xcode 5.1.1 (Apple clang 3.6) and a custom OSX clang 3.6 build. I
assume this means libc++ 3.6; I don't know how to check the libc++ version.

test.cpp:
#include <cstdlib>
#include <string>

template <typename T>
class MyAllocator {
public:
    typedef T value_type;

    // Uncomment to compile successfully. Should not be needed.
    // MyAllocator() {}

    template <typename U>
    MyAllocator(const MyAllocator<U>&& other) {}

    T* allocate(size_t n) {
        return static_cast<T*>(malloc(n * sizeof(T)));
    }

    void deallocate(T* p, size_t /*n*/) {
        free(p);
    }
};

int main(int /*argc*/, char* /*argv*/[]) {
    std::basic_string<char, std::char_traits<char>, MyAllocator<char>>
str("Hello, world!");
    return 0;
}

$ clang --version
Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6.0svn)
Target: x86_64-apple-darwin14.5.0
Thread model: posix

$ clang -Wall -std=c++11 -c test.cpp -o test.o
In file included from test.cpp:2:
In file included from
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439:
In file included from
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:628:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2136:31:
error: constructor for
'std::__1::__libcpp_compressed_pair_imp<std::__1::basic_string<char,
std::__1::char_traits<char>, MyAllocator<char> >::__rep, MyAllocator<char>, 2>'
must explicitly initialize the base class 'MyAllocator<char>' which does not
have a default constructor
    _LIBCPP_INLINE_VISIBILITY __libcpp_compressed_pair_imp() {}
                              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2315:31:
note: in instantiation of member function
'std::__1::__libcpp_compressed_pair_imp<std::__1::basic_string<char,
std::__1::char_traits<char>, MyAllocator<char> >::__rep, MyAllocator<char>,
2>::__libcpp_compressed_pair_imp' requested here
    _LIBCPP_INLINE_VISIBILITY __compressed_pair() {}
                              ^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:1387:31:
note: in instantiation of member function
'std::__1::__compressed_pair<std::__1::basic_string<char,
std::__1::char_traits<char>, MyAllocator<char> >::__rep, MyAllocator<char>
>::__compressed_pair' requested here
    _LIBCPP_INLINE_VISIBILITY basic_string(const value_type* __s);
                              ^
test.cpp:25:72: note: in instantiation of member function
'std::__1::basic_string<char, std::__1::char_traits<char>, MyAllocator<char>
>::basic_string' requested here
    std::basic_string<char, std::char_traits<char>, MyAllocator<char>>
str("Hello, world!");
                                                                       ^
test.cpp:5:7: note: 'MyAllocator<char>' declared here
class MyAllocator {
      ^
1 error generated.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20150925/362fcb84/attachment.html>


More information about the llvm-bugs mailing list