{"id":15047263,"url":"https://github.com/martinmoene/observer-ptr-lite","last_synced_at":"2025-06-16T23:04:35.869Z","repository":{"id":53744980,"uuid":"45994408","full_name":"martinmoene/observer-ptr-lite","owner":"martinmoene","description":"observer-ptr - An observer_ptr for C++98 and later in a single-file header-only library (Extensions for Library Fundamentals, v2, v3)","archived":false,"fork":false,"pushed_at":"2024-06-01T15:06:53.000Z","size":163,"stargazers_count":36,"open_issues_count":0,"forks_count":6,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-04-10T01:44:52.206Z","etag":null,"topics":["cpp17","cpp98","header-only","no-dependencies","observer-ptr","single-file"],"latest_commit_sha":null,"homepage":"","language":"C++","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsl-1.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/martinmoene.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.txt","contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-11-11T16:41:03.000Z","updated_at":"2024-06-01T15:06:57.000Z","dependencies_parsed_at":"2024-06-01T17:04:13.924Z","dependency_job_id":"dd5cabe3-2908-4afb-ac85-4561f4df7dac","html_url":"https://github.com/martinmoene/observer-ptr-lite","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/martinmoene/observer-ptr-lite","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2Fobserver-ptr-lite","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2Fobserver-ptr-lite/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2Fobserver-ptr-lite/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2Fobserver-ptr-lite/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/martinmoene","download_url":"https://codeload.github.com/martinmoene/observer-ptr-lite/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/martinmoene%2Fobserver-ptr-lite/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":260256240,"owners_count":22981806,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cpp17","cpp98","header-only","no-dependencies","observer-ptr","single-file"],"created_at":"2024-09-24T20:55:51.127Z","updated_at":"2025-06-16T23:04:35.847Z","avatar_url":"https://github.com/martinmoene.png","language":"C++","funding_links":[],"categories":[],"sub_categories":[],"readme":"# observer_ptr\u003c\u003e for C++98 up\n\n[![Language](https://img.shields.io/badge/C%2B%2B-98/11/14/17-blue.svg)](https://en.wikipedia.org/wiki/C%2B%2B#Standardization) [![License](https://img.shields.io/badge/license-BSL-blue.svg)](https://opensource.org/licenses/BSL-1.0) [![Build Status](https://github.com/martinmoene/observer-ptr-lite/actions/workflows/ci.yml/badge.svg)](https://github.com/martinmoene/observer-ptr-lite/actions/workflows/ci.yml) [![Build status](https://ci.appveyor.com/api/projects/status/6icjotc617wmhljt/branch/master?svg=true)](https://ci.appveyor.com/project/martinmoene/observer-ptr/branch/master) [![Version](https://badge.fury.io/gh/martinmoene%2Fobserver-ptr.svg)](https://github.com/martinmoene/observer-ptr/releases) [![download](https://img.shields.io/badge/latest-download-blue.svg)](https://raw.githubusercontent.com/martinmoene/observer-ptr/master/include/nonstd/observer_ptr.h) [![Conan](https://img.shields.io/badge/on-conan-blue.svg)](https://conan.io/center/observer-ptr-lite) [![Vcpkg](https://img.shields.io/badge/on-vcpkg-blue.svg)](https://vcpkg.link/ports/observer-ptr-lite) [![Try it online](https://img.shields.io/badge/on-wandbox-blue.svg)](https://wandbox.org/permlink/31XA2ReBGNDnplYK) [![Try it on godbolt online](https://img.shields.io/badge/on-godbolt-blue.svg)](https://godbolt.org/z/jwVx39)\n\n*observer-ptr* is a single-file header-only library with a variant of std::experimental::observer_ptr [[1](#ref1)] for C++98 and later.\n\n**Contents**  \n- [Example usage](#example-usage)\n- [In a nutshell](#in-a-nutshell)\n- [License](#license)\n- [Dependencies](#dependencies)\n- [Installation](#installation)\n- [Building the tests](#building-the-tests)\n- [Synopsis](#synopsis)\n- [Other open source implementations](#other-open-source-implementations)\n- [Notes and references](#notes-and-references)\n- [Appendix](#appendix)\n\n\nExample usage\n-------------\n```Cpp\n#include \"nonstd/observer_ptr.hpp\"\n\nusing namespace nonstd;\n\nvoid use( observer_ptr\u003cint\u003e p )\n{\n    assert( *p == 42 );\n}\n\nint main()\n{\n    int a = 42;\n    observer_ptr\u003cint\u003e p( \u0026a );\n    use( p );\n}\n```\n\n### Compile and run\n\n```\nprompt\u003eg++ -std=c++03 -Wall -I../include -o 01-basic.exe 01-basic.cpp \u0026\u0026 01-basic.exe\n```\n\nIn a nutshell\n-------------\n**observer-ptr** is an implementation of the *world’s dumbest smart pointer* for C++98 and higher. It takes no ownership responsibility for the object it *observes* or *watches* and is intended as a near drop-in replacement for raw pointer types. As a vocabulary type it indicates intended use, easing code reading ([Note 1](#note1)).\n\nClass template `observer_ptr\u003c\u003e` has been proposed for inclusion into the C++ standard [1] and is part of Extensions for Library Fundamentals v2/v3 [[2](#ref2)][[3](#ref3)].\n\nThe *observer-ptr* of this project can be used with probably any clang, g++ or MSVC compiler. It has been tested with clang 3.4, g++ 5.2 and with VC6 (VS6, no comparison of observers), VC8 (VS2005), VC10 (VS2010), VC11 (VS2012), VC12 (VS2013), VC14 (VS2015).\n\nLicense\n-------\n*observer-ptr* is distributed under the [Boost Software License](LICENSE.txt).\n \n\nDependencies\n------------\n*observer-ptr* has no other dependencies than the [C++ standard library](http://en.cppreference.com/w/cpp/header).\n\n\nInstallation\n------------\n*observer-ptr* is a single-file header-only library. Put [observer_ptr.h](include/nonstd/observer_ptr.h) in the [include](include) folder directly into the project source tree or somewhere reachable from your project.\n\n\nBuilding the tests\n------------------\nTo build the tests you need:\n\n- [CMake](http://cmake.org), version 2.8.7 or later to be installed and in your PATH.\n- A [suitable compiler](#reported-to-work-with). \n\nThe [*lest* test framework](https://github.com/martinmoene/lest)  is included in the [test folder](test).\n \nThe following steps assume that the [*observer-ptr* source code](https://github.com/martinmoene/observer-ptr) has been cloned into a directory named `c:\\observer-ptr`.\n\n1. Create a directory for the build outputs for a particular architecture.  \nHere we use c:\\observer-ptr\\build-win-x86-vc10.\n\n        cd c:\\observer-ptr\n        md build-win-x86-vc10\n        cd build-win-x86-vc10\n\n2. Configure CMake to use the compiler of your choice (run `cmake --help` for a list).\n\n        cmake -G \"Visual Studio 10 2010\" ..\n\n3. Build the test suite in the Debug configuration (alternatively use Release).    \n\n        cmake --build . --config Debug\n\n4. Run the test suite.    \n\n        ctest -V -C Debug\n\nAll tests should pass, indicating your platform is supported and you are ready to use *observer-ptr*.\n\n\nSynopsis\n--------\n\n**Contents**  \n[Documentation of `std::experimental::observer_ptr`](#documentation-of-stdobserver_ptr)  \n[Configuration macros](#configuration-macros)  \n\n### Documentation of `std::experimental::observer_ptr`\n\nDepending on the compiler and C++-standard used, `nonstd::observer_ptr` behaves less or more like `std::experimental::observer_ptr`. To get an idea of the capabilities of `nonstd::observer_ptr` with your configuration, look at the output of the [tests](test/observer_ptr.t.cpp), issuing `observer_ptr-main.t --pass @`. For `std::experimental::observer_ptr`, see its [documentation at cppreference](https://en.cppreference.com/w/cpp/experimental/observer_ptr) [[5](#ref5)].  \n\n### Configuration macros\n\n#### Standard selection macro\n\\-D\u003cb\u003ensop\\_CPLUSPLUS\u003c/b\u003e=199711L  \nDefine this macro to override the auto-detection of the supported C++ standard, or if your compiler does not set the `__cplusplus` macro correctly.\n\n#### Select `std::experimental::observer_ptr` or `nonstd::observer_ptr`\nAt default, *observer-ptr lite* uses `std::experimental::observer_ptr` if it is available and lets you use it via namespace `nonstd`. You can however override this default and explicitly request to use `std::experimental::observer_ptr` or *observer-ptr lite*'s `nonstd::observer_ptr` as `nonstd::observer_ptr` via the following macros.\n\n-D\u003cb\u003ensop\\_CONFIG\\_SELECT\\_OBSERVER_PTR\u003c/b\u003e=nsop_OBSERVER_PTR_DEFAULT  \nDefine this to `nsop_OBSERVER_PTR_STD` to select `std::experimental::observer_ptr` as `nonstd::observer_ptr`. Define this to `nsop_OBSERVER_PTR_NONSTD` to select `nonstd::observer_ptr` as `nonstd::observer_ptr`. Default is undefined, which has the same effect as defining to `nsop_OBSERVER_PTR_DEFAULT`.\n\n#### Conversions\n\n\\-D\u003cb\u003ensop\\_CONFIG\\_ALLOW\\_IMPLICIT\\_CONVERSION\\_FROM_SMART\\_PTR\u003c/b\u003e=0  \nAllow implicit conversion from `std::unique_ptr` and `std::shared_ptr`. This is an extension to the proposal. Each of these implicit conversions can also be activated separately, see below. Default is 0.\n\n\\-D\u003cb\u003ensop\\_CONFIG\\_ALLOW\\_IMPLICIT\\_CONVERSION\\_FROM\\_UNIQUE\\_PTR\u003c/b\u003e=0  \nAllow implicit conversion from `std::unique_ptr`. This is an extension to the proposal. Default is 0.\n\n\\-D\u003cb\u003ensop\\_CONFIG\\_ALLOW\\_IMPLICIT\\_CONVERSION\\_FROM\\_SHARED\\_PTR\u003c/b\u003e=0  \nAllow implicit conversion from `std::shared_ptr`. This is an extension to the proposal. Default is 0.\n\n\\-D\u003cb\u003ensop\\_CONFIG\\_ALLOW\\_IMPLICIT\\_CONVERSION\\_TO\\_UNDERLYING\\_TYPE\u003c/b\u003e=0  \nThe proposed `observer_ptr` provides [explicit conversions](http://en.cppreference.com/w/cpp/language/explicit) to `bool` and to the underlying type. Explicit conversion is not available from pre-C++11 compilers. To prevent problems due to unexpected [implicit conversions](http://en.cppreference.com/w/cpp/language/implicit_cast) to `bool` or to the underlying type, this library does not provide these implicit conversions at default. If you still want them, define this macro to 1. Without these implicit conversions enabled, a conversion to bool via the [safe bool idiom](https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Safe_bool) is provided. Default is 0.\n\n#### Compile-time tests\n\n\\-D\u003cb\u003ensop\\_CONFIG\\_CONFIRMS\\_COMPILATION\\_ERRORS\u003c/b\u003e=0  \nDefine this macro to 1 to experience the by-design compile-time errors of the *observer-ptr* components in the test suite. Default is 0.\n\n\nOther open source implementations\n---------------------------------\n- Anthony Williams. [object_ptr - a safer replacement for raw pointers](https://www.justsoftwaresolutions.co.uk/cplusplus/object_ptr.html) (Boost License).\n- Mário Feroldi. [observer_ptr implementation in C++17 (Library Fundamentals TS v2)](https://github.com/feroldi/observer_ptr) (MIT License).\n- Joseph Thomson. [observer\u003cT\u003e and optional_ref\u003cT\u003e for the Guideline Support Library](https://github.com/hpesoj/gsl-pointers) (MIT License).\n\n\nNotes and references\n--------------------\n### Notes\n\u003ca id=\"note1\"\u003e\u003c/a\u003eNote 1. This conclusion may be challenged if the coding style ensures that *any raw pointer* is a *non-owning pointer* [[4](#ref4)].  \n\n### References\n\u003ca id=\"ref1\"\u003e\u003c/a\u003e[1] Walter E. Brown. [N3840: A Proposal for the World’s Dumbest Smart Pointer, v4](http://wg21.link/n4282) ([v1](http://wg21.link/n3514), [v2](http://wg21.link/n3740), [v3](http://wg21.link/n3840), [v4](http://wg21.link/n4282) (PDF). 19 December 2012 - 7 November 2014.  \n\u003ca id=\"ref2\"\u003e\u003c/a\u003e[2] N4481: Tentative Working Draft, C++ Extensions for Library Fundamentals, Version 2, [Section 4.2 Non-owning pointers](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4481.html#memory.observer.ptr). 12 April 2015.  \n\u003ca id=\"ref3\"\u003e\u003c/a\u003e[3] N4758: Working Draft, C++ Extensions for Library Fundamentals, Version 3, [Section 5.2 Non-owning pointers](https://rawgit.com/cplusplus/fundamentals-ts/v3/fundamentals-ts.html#memory.observer.ptr). 13 November 2018.  \n\u003ca id=\"ref4\"\u003e\u003c/a\u003e[4] Bjarne Stroustrup. [P1408: Abandon observer_ptr](http://wg21.link/p1408). 4 January 2018.  \n\u003ca id=\"ref5\"\u003e\u003c/a\u003e[5] ISO C++ Standard - Future Proposals. [shared_ptr and unique_ptr should both implicitly convert to observer_ptr](https://groups.google.com/a/isocpp.org/forum/#!msg/std-proposals/7gsM7DaPWds/wlvOWH06CQAJ). 24 October 2018.  \n\u003ca id=\"ref6\"\u003e\u003c/a\u003e[6] Joseph Thomson. [Pointers and the C++ Core Guidelines](https://github.com/hpesoj/gsl-pointers#pointers-and-the-c-core-guidelines). 9 February 2017.  \n\u003ca id=\"ref7\"\u003e\u003c/a\u003e[7] C++ Core Guidelines. [Issue 847: Pointers and the C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/issues/847). 9 February 2017.  \n\u003ca id=\"ref8\"\u003e\u003c/a\u003e[8] Boost developers' mailing list. [Is there any interest in non-owning pointer-like types?](http://boost.2283326.n4.nabble.com/Is-there-any-interest-in-non-owning-pointer-like-types-tp4691421.html) 1 February 2017.  \n\u003ca id=\"ref9\"\u003e\u003c/a\u003e[9] cppreference.com. [std\u0026#58;\u0026#58;experimental\u0026#58;\u0026#58;observer_ptr](http://en.cppreference.com/w/cpp/experimental/observer_ptr).  \n\n\nAppendix\n--------\n\n### A.1 Compile-time information\n\nThe version of *observer-ptr lite* is available via tag `[.version]`. The following tags are available for information on the compiler and on the C++ standard library used: `[.compiler]`, `[.stdc++]`, `[.stdlanguage]` and `[.stdlibrary]`.\n\n### A.2 Observer Ptr test specification\n\n```\nDisallows to delete the observer_ptr unless implicit conversion allowed\nDisallows construction from an observer_ptr of incompatible type\nDisallows implicit conversion to bool unless implicit conversion allowed\nDisallows implicit conversion to underlying type unless implicit conversion allowed\nDisallows comparison to an observer_ptr with a different underlying type\nAllows default construction\nAllows construction from nullptr\nAllows construction from a non-null pointer\nAllows construction from an observer_ptr of compatible type\nAllows implicit move-construction from a std::unique_ptr\u003c\u003e [smart-ptr][extension]\nAllows implicit construction from a std::shared_ptr\u003c\u003e [smart-ptr][extension]\nAllows to retrieve the pointer\nAllows to retrieve the value pointed to\nAllows to retrieve the member pointed to\nAllows to test for a non-null pointer via conversion to bool\nAllows to convert to the observed pointer [underlying-type][extension]\nAllows to release to stop observing\nAllows to reset to stop observing\nAllows to reset to observe another pointer\nAllows to swap two observers\nSpecialized: Allows to swap two observers\nSpecialized: Allows to make an observer\nSpecialized: Allows to compare if an observer is equal to another observer\nSpecialized: Allows to compare if an observer is equal to another observer with a related watched type\nSpecialized: Allows to compare if an observer is not equal to another observer\nSpecialized: Allows to compare if an observer is not equal to another observer with a related watched type\nSpecialized: Allows to compare if an observer is equal to nullptr\nSpecialized: Allows to compare if an observer is not equal to nullptr\nSpecialized: Allows to compare if an observer is less than another observer\nSpecialized: Allows to compare if an observer is less than another observer with a related watched type\nSpecialized: Allows to compare if an observer is less than or equal to another observer\nSpecialized: Allows to compare if an observer is less than or equal to another observer with a related watched type\nSpecialized: Allows to compare if an observer is greater than another observer\nSpecialized: Allows to compare if an observer is greater than another observer with a related watched type\nSpecialized: Allows to compare if an observer is greater than or equal to another observer\nSpecialized: Allows to compare if an observer is greater than or equal to another observer with a related watched type\nSpecialized: Allows to compute hash\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartinmoene%2Fobserver-ptr-lite","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmartinmoene%2Fobserver-ptr-lite","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmartinmoene%2Fobserver-ptr-lite/lists"}