Error handling with std::optional and private constructor

I’ve had a similar post on stackoverflow but it turned out the situation is a bit different with JUCE class so I’m asking again at the forum.

The thing I tried to do is initializing my own class with a static class method which calls a private constructor and return a std::optional object depending on whether some condition is met:

class SoundEngine
{
private:
    explicit SoundEngine(std::unique_ptr<juce::AudioDeviceManager> dvcMgr): deviceManager(std::move(dvcMgr))
    {
    }
public:
    static std::optional<SoundEngine> makeInstance()
    {
        auto deviceManager = std::make_unique<juce::AudioDeviceManager>();
        auto err = deviceManager->initialiseWithDefaultDevices(2, 2);
        if (err.isNotEmpty())
        {
            juce::Logger::writeToLog(err);
            return std::nullopt;
        }
        else
        {
            auto engine = ::SoundEngine(std::move(deviceManager));
            auto opt0 = std::make_optional<SoundEngine>(std::move(engine));
            auto opt1 = std::make_optional<SoundEngine>(SoundEngine{std::move(deviceManager)});
            auto opt2 = std::make_optional<SoundEngine>(std::move(deviceManager));
            auto opt3 = std::make_optional<SoundEngine>({std::move(deviceManager)});

            return std::make_optional(std::move(engine)); //return0
            return std::make_optional<SoundEngine>(SoundEngine{std::move(deviceManager)}); //return1
            return std::make_optional<SoundEngine>(std::move(deviceManager)); //return2
            return std::make_optional<SoundEngine>({std::move(deviceManager)}); //return3
        }
    }

private:
    std::unique_ptr<juce::AudioDeviceManager> deviceManager;
    juce::AudioFormatManager formatManager;
};

those 4 opts and 4 returns are where I tried calling all 3 signatures provided by std::make_optional:

template <class _Tp>
_LIBCPP_INLINE_VISIBILITY constexpr
optional<decay_t<_Tp>> make_optional(_Tp&& __v)
{
    return optional<decay_t<_Tp>>(_VSTD::forward<_Tp>(__v));
}

template <class _Tp, class... _Args>
_LIBCPP_INLINE_VISIBILITY constexpr
optional<_Tp> make_optional(_Args&&... __args)
{
    return optional<_Tp>(in_place, _VSTD::forward<_Args>(__args)...);
}

template <class _Tp, class _Up, class... _Args>
_LIBCPP_INLINE_VISIBILITY constexpr
optional<_Tp> make_optional(initializer_list<_Up> __il,  _Args&&... __args)
{
    return optional<_Tp>(in_place, __il, _VSTD::forward<_Args>(__args)...);
}

if I understand it correctly, opt0, opt1, return0 and return1 refer to signature1, since make_optional receive a rvalue of type SoundEngine;
with opt2 and return2, opt3 and return3, I meant to call with the corresponding make_optional signature receiving _Ars&&... and initializer_list<_Up> respectively (the 2nd and 3rd signalture above).

The observations are

  1. with the formatManager member and a private constructor, all these operations failed.
  2. If changing the constructor to public and retain formatManager member, all operations except opt2 and return2 failed.
  3. if commenting out formatManager and leave the constructor as private, opt0, opt1, return0 and return 1 passed and the others not.
  4. if commenting out formatManager and changing the constructor to public, only opt3 and return3 failed.
  5. in observation4, if I removed explicit, return3 can implicitly call the constructor now, but opt3 still cannot.

The error message for observation 1:

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1388:12: error: no matching conversion for functional-style cast from 'SoundEngine' to 'optional<decay_t<SoundEngine>>' (aka 'optional<SoundEngine>')
    return optional<decay_t<_Tp>>(_VSTD::forward<_Tp>(__v));
           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/yhe/Develop/Company/SoundX/simple-daw-engine/native_engine/src/sound_engine.h:29:30: note: in instantiation of function template specialization 'std::make_optional<SoundEngine>' requested here
            auto opt0 = std::make_optional<SoundEngine>(std::move(engine));
                             ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:694:41: note: candidate constructor not viable: no known conversion from 'SoundEngine' to 'const std::optional<SoundEngine>' for 1st argument
    _LIBCPP_INLINE_VISIBILITY constexpr optional(const optional&) = default;
                                        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:696:41: note: candidate constructor not viable: no known conversion from 'SoundEngine' to 'std::nullopt_t' for 1st argument
    _LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {}
                                        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:706:24: note: candidate template ignored: substitution failure [with _InPlaceT = SoundEngine, _Args = <>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
    constexpr explicit optional(_InPlaceT, _Args&&... __args)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:720:15: note: candidate template ignored: substitution failure [with _Up = SoundEngine]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
    constexpr optional(_Up&& __v)
              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:727:24: note: candidate template ignored: substitution failure [with _Up = SoundEngine]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
    constexpr explicit optional(_Up&& __v)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:735:35: note: candidate template ignored: could not match 'optional<type-parameter-0-0>' against 'SoundEngine'
    _LIBCPP_CONSTEXPR_AFTER_CXX17 optional(const optional<_Up>& __v)
                                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:743:44: note: candidate template ignored: could not match 'optional<type-parameter-0-0>' against 'SoundEngine'
    _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit optional(const optional<_Up>& __v)
                                           ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:753:35: note: candidate template ignored: could not match 'optional<type-parameter-0-0>' against 'SoundEngine'
    _LIBCPP_CONSTEXPR_AFTER_CXX17 optional(optional<_Up>&& __v)
                                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:761:44: note: candidate template ignored: could not match 'optional<type-parameter-0-0>' against 'SoundEngine'
    _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit optional(optional<_Up>&& __v)
                                           ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:693:41: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
    _LIBCPP_INLINE_VISIBILITY constexpr optional() noexcept {}
                                        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:713:24: note: candidate constructor template not viable: requires at least 2 arguments, but 1 was provided
    constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1395:12: error: no matching constructor for initialization of 'optional<SoundEngine>'
    return optional<_Tp>(in_place, _VSTD::forward<_Args>(__args)...);
           ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/yhe/Develop/Company/SoundX/simple-daw-engine/native_engine/src/sound_engine.h:30:30: note: in instantiation of function template specialization 'std::make_optional<SoundEngine, SoundEngine>' requested here
            auto opt1 = std::make_optional<SoundEngine>(SoundEngine{std::move(deviceManager)});
                             ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:706:24: note: candidate template ignored: substitution failure [with _InPlaceT = std::in_place_t, _Args = <SoundEngine>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
    constexpr explicit optional(_InPlaceT, _Args&&... __args)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:713:24: note: candidate template ignored: could not match 'initializer_list<type-parameter-0-0>' against 'SoundEngine'
    constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:694:41: note: candidate constructor not viable: requires 1 argument, but 2 were provided
    _LIBCPP_INLINE_VISIBILITY constexpr optional(const optional&) = default;
                                        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:696:41: note: candidate constructor not viable: requires 1 argument, but 2 were provided
    _LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {}
                                        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:720:15: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    constexpr optional(_Up&& __v)
              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:727:24: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    constexpr explicit optional(_Up&& __v)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:735:35: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    _LIBCPP_CONSTEXPR_AFTER_CXX17 optional(const optional<_Up>& __v)
                                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:743:44: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit optional(const optional<_Up>& __v)
                                           ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:753:35: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    _LIBCPP_CONSTEXPR_AFTER_CXX17 optional(optional<_Up>&& __v)
                                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:761:44: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit optional(optional<_Up>&& __v)
                                           ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:693:41: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
    _LIBCPP_INLINE_VISIBILITY constexpr optional() noexcept {}
                                        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1395:12: error: no matching constructor for initialization of 'optional<SoundEngine>'
    return optional<_Tp>(in_place, _VSTD::forward<_Args>(__args)...);
           ^             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/yhe/Develop/Company/SoundX/simple-daw-engine/native_engine/src/sound_engine.h:31:30: note: in instantiation of function template specialization 'std::make_optional<SoundEngine, std::unique_ptr<juce::AudioDeviceManager>>' requested here
            auto opt2 = std::make_optional<SoundEngine>(std::move(deviceManager));
                             ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:706:24: note: candidate template ignored: substitution failure [with _InPlaceT = std::in_place_t, _Args = <std::unique_ptr<juce::AudioDeviceManager>>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
    constexpr explicit optional(_InPlaceT, _Args&&... __args)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:713:24: note: candidate template ignored: could not match 'initializer_list' against 'unique_ptr'
    constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:694:41: note: candidate constructor not viable: requires 1 argument, but 2 were provided
    _LIBCPP_INLINE_VISIBILITY constexpr optional(const optional&) = default;
                                        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:696:41: note: candidate constructor not viable: requires 1 argument, but 2 were provided
    _LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {}
                                        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:720:15: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    constexpr optional(_Up&& __v)
              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:727:24: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    constexpr explicit optional(_Up&& __v)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:735:35: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    _LIBCPP_CONSTEXPR_AFTER_CXX17 optional(const optional<_Up>& __v)
                                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:743:44: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit optional(const optional<_Up>& __v)
                                           ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:753:35: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    _LIBCPP_CONSTEXPR_AFTER_CXX17 optional(optional<_Up>&& __v)
                                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:761:44: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit optional(optional<_Up>&& __v)
                                           ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:693:41: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
    _LIBCPP_INLINE_VISIBILITY constexpr optional() noexcept {}
                                        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1402:12: error: no matching constructor for initialization of 'optional<SoundEngine>'
    return optional<_Tp>(in_place, __il, _VSTD::forward<_Args>(__args)...);
           ^             ~~~~~~~~~~~~~~
/Users/yhe/Develop/Company/SoundX/simple-daw-engine/native_engine/src/sound_engine.h:32:30: note: in instantiation of function template specialization 'std::make_optional<SoundEngine, std::unique_ptr<juce::AudioDeviceManager>>' requested here
            auto opt3 = std::make_optional<SoundEngine>({std::move(deviceManager)});
                             ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:706:24: note: candidate template ignored: substitution failure [with _InPlaceT = std::in_place_t, _Args = <std::initializer_list<std::unique_ptr<juce::AudioDeviceManager>> &>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
    constexpr explicit optional(_InPlaceT, _Args&&... __args)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:713:24: note: candidate template ignored: substitution failure [with _Up = std::unique_ptr<juce::AudioDeviceManager>, _Args = <>]: no member named '_EnableIfImpl' in 'std::_MetaBase<false>'
    constexpr explicit optional(in_place_t, initializer_list<_Up> __il, _Args&&... __args)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:694:41: note: candidate constructor not viable: requires 1 argument, but 2 were provided
    _LIBCPP_INLINE_VISIBILITY constexpr optional(const optional&) = default;
                                        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:696:41: note: candidate constructor not viable: requires 1 argument, but 2 were provided
    _LIBCPP_INLINE_VISIBILITY constexpr optional(nullopt_t) noexcept {}
                                        ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:720:15: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    constexpr optional(_Up&& __v)
              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:727:24: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    constexpr explicit optional(_Up&& __v)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:735:35: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    _LIBCPP_CONSTEXPR_AFTER_CXX17 optional(const optional<_Up>& __v)
                                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:743:44: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit optional(const optional<_Up>& __v)
                                           ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:753:35: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    _LIBCPP_CONSTEXPR_AFTER_CXX17 optional(optional<_Up>&& __v)
                                  ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:761:44: note: candidate constructor template not viable: requires single argument '__v', but 2 arguments were provided
    _LIBCPP_CONSTEXPR_AFTER_CXX17 explicit optional(optional<_Up>&& __v)
                                           ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:693:41: note: candidate constructor not viable: requires 0 arguments, but 2 were provided
    _LIBCPP_INLINE_VISIBILITY constexpr optional() noexcept {}
                                        ^
In file included from /Users/yhe/Develop/Company/SoundX/simple-daw-engine/native_engine/main.cpp:7:
/Users/yhe/Develop/Company/SoundX/simple-daw-engine/native_engine/src/sound_engine.h:34:20: error: no matching function for call to 'make_optional'
            return std::make_optional(std::move(engine));
                   ^~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1393:15: note: candidate template ignored: couldn't infer template argument '_Tp'
optional<_Tp> make_optional(_Args&&... __args)
              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1386:24: note: candidate template ignored: substitution failure [with _Tp = SoundEngine]
optional<decay_t<_Tp>> make_optional(_Tp&& __v)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1400:15: note: candidate template ignored: could not match 'initializer_list<type-parameter-0-1>' against 'SoundEngine'
optional<_Tp> make_optional(initializer_list<_Up> __il,  _Args&&... __args)
              ^
In file included from /Users/yhe/Develop/Company/SoundX/simple-daw-engine/native_engine/main.cpp:7:
/Users/yhe/Develop/Company/SoundX/simple-daw-engine/native_engine/src/sound_engine.h:35:20: error: no matching function for call to 'make_optional'
            return std::make_optional<SoundEngine>(SoundEngine{std::move(deviceManager)});
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1386:24: note: candidate template ignored: substitution failure [with _Tp = SoundEngine]
optional<decay_t<_Tp>> make_optional(_Tp&& __v)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1393:15: note: candidate template ignored: substitution failure [with _Tp = SoundEngine, _Args = <SoundEngine>]
optional<_Tp> make_optional(_Args&&... __args)
              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1400:15: note: candidate template ignored: could not match 'initializer_list<type-parameter-0-1>' against 'SoundEngine'
optional<_Tp> make_optional(initializer_list<_Up> __il,  _Args&&... __args)
              ^
In file included from /Users/yhe/Develop/Company/SoundX/simple-daw-engine/native_engine/main.cpp:7:
/Users/yhe/Develop/Company/SoundX/simple-daw-engine/native_engine/src/sound_engine.h:36:20: error: no matching function for call to 'make_optional'
            return std::make_optional<SoundEngine>(std::move(deviceManager));
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1386:24: note: candidate function template not viable: no known conversion from 'typename remove_reference<unique_ptr<AudioDeviceManager> &>::type' (aka 'std::unique_ptr<juce::AudioDeviceManager>') to 'SoundEngine' for 1st argument
optional<decay_t<_Tp>> make_optional(_Tp&& __v)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1393:15: note: candidate template ignored: substitution failure [with _Tp = SoundEngine, _Args = <std::unique_ptr<juce::AudioDeviceManager>>]
optional<_Tp> make_optional(_Args&&... __args)
              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1400:15: note: candidate template ignored: could not match 'initializer_list' against 'unique_ptr'
optional<_Tp> make_optional(initializer_list<_Up> __il,  _Args&&... __args)
              ^
In file included from /Users/yhe/Develop/Company/SoundX/simple-daw-engine/native_engine/main.cpp:7:
/Users/yhe/Develop/Company/SoundX/simple-daw-engine/native_engine/src/sound_engine.h:37:20: error: no matching function for call to 'make_optional'
            return std::make_optional<SoundEngine>({std::move(deviceManager)});
                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1386:24: note: candidate function template not viable: cannot convert initializer list argument to 'SoundEngine'
optional<decay_t<_Tp>> make_optional(_Tp&& __v)
                       ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1393:15: note: candidate template ignored: substitution failure [with _Tp = SoundEngine]: deduced incomplete pack <(no value)> for template parameter '_Args'
optional<_Tp> make_optional(_Args&&... __args)
              ^
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.3.sdk/usr/include/c++/v1/optional:1400:15: note: candidate template ignored: substitution failure [with _Tp = SoundEngine, _Up = std::unique_ptr<juce::AudioDeviceManager>, _Args = <>]
optional<_Tp> make_optional(initializer_list<_Up> __il,  _Args&&... __args)
              ^
2 warnings and 8 errors generated.
make[3]: *** [CMakeFiles/test_main.dir/main.cpp.o] Error 1
make[2]: *** [CMakeFiles/test_main.dir/all] Error 2
make[1]: *** [CMakeFiles/test_main.dir/rule] Error 2
make: *** [test_main] Error 2

I had several questions:

  1. opt0 , opt1 , return0 and return1 refer to signature1, opt2 and return2 refer to signalture2, opt3 and return3 refer to signature3. Is this assumption correct or not?
  2. signature1 receives a temporary object so it doesn’t need a constructor; whereas signature2 and signature3 both use in-place initialization so optional<SoundEngine> would require a public constructor of SoundEngine. Is that correct?
  3. how to explain these observations?
  4. how to fix the issue and achieve the goal in the title that using std::optional and a private constructor for error handling