forked from GPUOpen-Drivers/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[libc++] Fix allocate_shared when used with an explicitly convertible…
… allocator When the allocator is only explicitly convertible from other specializations of itself, the new version of std::allocate_shared would not work because it would try to do an implicit conversion. This patch fixes the problem and adds a test so that we don't fall into the same trap in the future.
- Loading branch information
Showing
2 changed files
with
34 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
....smartptr.shared/util.smartptr.shared.create/allocate_shared.explicit_conversion.pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
// Make sure that std::allocate_shared works with an allocator type that is | ||
// only explicitly convertible from another specialization of itself. | ||
|
||
#include <cassert> | ||
#include <cstddef> | ||
#include <memory> | ||
|
||
template <class T> | ||
struct ExplicitAllocator { | ||
ExplicitAllocator() = default; | ||
template <class U> | ||
explicit ExplicitAllocator(ExplicitAllocator<U>) { } | ||
|
||
using value_type = T; | ||
T* allocate(std::size_t n) { return std::allocator<T>().allocate(n); } | ||
void deallocate(T* ptr, std::size_t n) { return std::allocator<T>().deallocate(ptr, n); } | ||
}; | ||
|
||
int main(int, char**) { | ||
std::shared_ptr<int> ptr = std::allocate_shared<int>(ExplicitAllocator<int>(), 0); | ||
(void)ptr; | ||
|
||
return 0; | ||
} |