Skip to content

Commit

Permalink
Change pre-C++11 conversions (Thanks to Phil Nash)
Browse files Browse the repository at this point in the history
For pre-C++11 use "explicit" conversion to safe-bool or implicit conversion to pointer.

With the "explicit" conversion to safe-bool there's no conversion to pointer.
With the implicit conversion to pointer enabled it also acts as implicit conversion to bool.
  • Loading branch information
martinmoene committed Nov 13, 2015
1 parent d7fe60e commit 545b44f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 19 deletions.
25 changes: 7 additions & 18 deletions include/nonstd/observer_ptr.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,24 +156,7 @@ class observer_ptr
{
return ptr != nop_NULLPTR;
}
#elif nop_FEATURE_ALLOW_IMPLICIT_CONVERSION
// note: operator safe_bool can't coexist with implicit conversion to pointer

nop_constexpr14 operator bool() const nop_noexcept
{
return ptr != nop_NULLPTR;
}
#else
typedef void (observer_ptr::*safe_bool)() const;
void this_type_does_not_support_comparisons() const {}

operator safe_bool() const
{
return ptr != nop_NULLPTR ? &observer_ptr::this_type_does_not_support_comparisons : 0;
}
#endif

#if nop_HAVE_EXPLICIT_CONVERSION
nop_constexpr14 explicit operator pointer() const nop_noexcept
{
return ptr;
Expand All @@ -185,7 +168,13 @@ class observer_ptr
return ptr;
}
#else
// note: a private implicit conversion to pointer can't coexist with operator safe_bool.
typedef void (observer_ptr::*safe_bool)() const;
void this_type_does_not_support_comparisons() const {}

nop_constexpr14 operator safe_bool() const nop_noexcept
{
return ptr != nop_NULLPTR ? &observer_ptr::this_type_does_not_support_comparisons : 0;
}
#endif

nop_constexpr14 pointer release() nop_noexcept
Expand Down
2 changes: 1 addition & 1 deletion test/observer_ptr.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ CASE( "Disallows implicit conversion to underlying type unless implicit conversi
#endif
}

CASE( "Disallows comparison to a observer_ptr with a different underlying type" )
CASE( "Disallows comparison to an observer_ptr with a different underlying type" )
{
#if nop_CONFIG_CONFIRMS_COMPILATION_ERRORS
int a = 7; observer_ptr<int > ap( &a );
Expand Down

0 comments on commit 545b44f

Please sign in to comment.