Skip to content

Instantly share code, notes, and snippets.

@maxtruxa
Last active August 29, 2015 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxtruxa/97ba44d24da534dfbad6 to your computer and use it in GitHub Desktop.
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
#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;
}
@maxtruxa
Copy link
Author

@maxtruxa
Copy link
Author

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