Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow copy/move construction for classes derived from expected #157

Open
SomeshDaga opened this issue Jan 9, 2024 · 1 comment
Open

Comments

@SomeshDaga
Copy link

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
Copy link
Author

@TartanLlama Please let me know if the issue and fix makes sense

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant