TartanLlama / expected

C++11/14/17 std::expected with functional-style extensions

Home Page:https://tl.tartanllama.xyz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Allow copy/move construction for classes derived from expected

SomeshDaga opened this issue · comments

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;
}

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