Skip to content

Make smart pointer constructors (e.g. wil::com_ptr) constexpr when initializing to nullptr #535

@fredemmott

Description

@fredemmott

e.g. something along the lines of:

diff --git a/include/wil/com.h b/include/wil/com.h
index 2492611..33e126c 100644
--- a/include/wil/com.h
+++ b/include/wil/com.h
@@ -215,12 +215,12 @@ public:
     //! @{
 
     //! Default constructor (holds nullptr).
-    com_ptr_t() WI_NOEXCEPT : m_ptr(nullptr)
+    __WI_LIBCPP_CONSTEXPR com_ptr_t() WI_NOEXCEPT : m_ptr(nullptr)
     {
     }
 
     //! Implicit construction from nullptr_t (holds nullptr).
-    com_ptr_t(wistd::nullptr_t) WI_NOEXCEPT : com_ptr_t()
+    __WI_LIBCPP_CONSTEXPR com_ptr_t(wistd::nullptr_t) WI_NOEXCEPT : com_ptr_t()
     {
     }
 
@@ -234,7 +234,7 @@ public:
     }
 
     //! Copy-construction from a like `com_ptr_t` (copies and AddRef's the parameter).
-    com_ptr_t(const com_ptr_t& other) WI_NOEXCEPT : com_ptr_t(other.get())
+    __WI_LIBCPP_CONSTEXPR com_ptr_t(const com_ptr_t& other) WI_NOEXCEPT : com_ptr_t(other.get())
     {
     }
 
@@ -269,7 +269,7 @@ public:
     //! @{
 
     //! Assign to nullptr (releases the current pointer, holds nullptr).
-    com_ptr_t& operator=(wistd::nullptr_t) WI_NOEXCEPT
+    __WI_LIBCPP_CONSTEXPR com_ptr_t& operator=(wistd::nullptr_t) WI_NOEXCEPT
     {
         reset();
         return *this;
@@ -292,7 +292,7 @@ public:
     }
 
     //! Assign a like `com_ptr_t` (releases current pointer, copies and AddRef's the parameter).
-    com_ptr_t& operator=(const com_ptr_t& other) WI_NOEXCEPT
+    __WI_LIBCPP_CONSTEXPR com_ptr_t& operator=(const com_ptr_t& other) WI_NOEXCEPT
     {
         return operator=(other.get());
     }

Not sending a pull request due to build dependencies on nuget, separately installed detours etc instead of more isolated and common tools like vcpkg.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions