Last active
August 29, 2015 14:23
-
-
Save maxtruxa/97ba44d24da534dfbad6 to your computer and use it in GitHub Desktop.
VC++ bug: std::unique_ptr with std::is_same<element_type, pointer>::value == true
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
#include <iostream> | |
#include <memory> | |
int main() { | |
struct my_type { | |
void poke() { std::cout << "poke\n"; } | |
}; | |
typedef my_type* my_type_ptr; | |
struct my_deleter { | |
typedef my_type_ptr pointer; | |
void operator()(my_type_ptr p) { delete p; } | |
}; | |
typedef std::unique_ptr<my_type_ptr, my_deleter> my_unique_ptr; | |
my_unique_ptr x(new my_type); | |
x->poke(); | |
return 0; | |
} |
VS2013: The culprit seems to be std::unique_ptr::operator->()
:
pointer operator->() const _NOEXCEPT
{
return (_STD pointer_traits<pointer>::pointer_to(**this));
}
The C++ standard does not mandate this unnecessary complex implementation. Quoting the latest working draft (N4296) §20.8.1.2.4 unique_ptr observers
:
pointer operator->() const noexcept;
(3) Requires: get() != nullptr.
(4) Returns: get().
VC++'s std::unique_ptr::get()
looks like this:
pointer get() const _NOEXCEPT
{
return (this->_Myptr);
}
GCC and LLVM both use the obvious (and actually working) implementation of std::unique_ptr::operator->()
, that would look like this in VC++:
pointer operator->() const _NOEXCEPT
{
return (this->_Myptr);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
see online demo (GCC)