Skip to content

std::basic_string requires Allocator to have a default constructor #25307

@llvmbot

Description

@llvmbot
Bugzilla Link 24933
Resolution INVALID
Resolved on Sep 24, 2015 23:48
Version 3.6
OS All
Reporter LLVM Bugzilla Contributor
CC @mclow

Extended Description

Instantiating std::basic_string<char, std::char_traits, MyAllocator> 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
#include

template
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, MyAllocator> 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, MyAllocator >::__rep, MyAllocator, 2>' must explicitly initialize the base class 'MyAllocator' 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, MyAllocator >::__rep, MyAllocator, 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, MyAllocator >::__rep, MyAllocator >::__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, MyAllocator >::basic_string' requested here
std::basic_string<char, std::char_traits, MyAllocator> str("Hello, world!");
^
test.cpp:5:7: note: 'MyAllocator' declared here
class MyAllocator {
^
1 error generated.

Metadata

Metadata

Assignees

Labels

bugzillaIssues migrated from bugzillainvalidResolved as invalid, i.e. not a buglibc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions