Allow copy/move construction for classes derived from expected
SomeshDaga opened this issue · 1 comments
SomeshDaga commented
I would like to inherit from tl::expected
and add some custom functionality for my use case. However, I have noticed issues with copy/move construction in the derived class (i.e. the copy/move constructors are not getting called and more general constructor overloads are instead getting called). The following example with bool exemplifies the issue:
#include <tl/expected.hpp>
#include <iostream>
#include <string>
template <typename T, typename E>
class Expected : public tl::expected<T, E>
{
public:
Expected() : tl::expected<T,E>() {}
Expected(const Expected& e) : tl::expected<T,E>(e) {}
};
int main()
{
tl::expected<bool, std::string> base_e1;
tl::expected<bool, std::string> base_e2(base_e1);
// base_e1 and base_e2 are both false (as expected)
std::cout << std::boolalpha
<< "(base class) e1: " << base_e1.value()
<< ", e2: " << base_e2.value() << std::endl;
Expected<bool, std::string> derived_e1;
Expected<bool, std::string> derived_e2(derived_e1);
// derived_e1 is false and derived_e2 is true (uh-oh)
std::cout << std::boolalpha
<< "(derived class) e1: " << derived_e1.value()
<< ", e2: " << derived_e2.value() << std::endl;
}
SomeshDaga commented
@TartanLlama Please let me know if the issue and fix makes sense