I'm currently learning OpenGL and while writing a shader abstraction class I chose std::optional
for error handling. Now to prevent accidental double freeing, I'm removing the copy constructors with
// Shader.h
Shader(const Shader&) = delete;
Shader& operator=(const Shader&) = delete;
but when returning
// Shader.cpp
return std::make_optional<Shader>(Shader(id));
in the static function fromSrc
it gives me a compile error
Error (active) E0304 no instance of overloaded function "std::make_optional" matches the argument list LearnOpengl *\LearnOpengl\LearnOpengl\src\util\Shader.cpp 90
I'm using Visual Studio 2022 (MSVC v143) with c++17
Edit: I was told to implement a move constructor, is this a good implementation?
Shader::Shader(Shader&& other) noexcept
: m_Id(std::exchange(other.m_Id, 0)),
m_Destroyed(std::exchange(other.m_Destroyed, true)),
m_Uniforms(std::exchange(other.m_Uniforms, {})) {}
Shader& Shader::operator=(Shader other) noexcept {
std::swap(m_Id, other.m_Id);
std::swap(m_Destroyed, other.m_Destroyed);
std::swap(m_Uniforms, other.m_Uniforms);
return *this;
}