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

syntax tl::expected for constructor member initializer list ? #164

Open
ArchNemSyS opened this issue Feb 5, 2025 · 1 comment
Open

syntax tl::expected for constructor member initializer list ? #164

ArchNemSyS opened this issue Feb 5, 2025 · 1 comment

Comments

@ArchNemSyS
Copy link

ArchNemSyS commented Feb 5, 2025

Trying to use TL::Expected in a Qt project as Qt doesn't play well with exceptions and is quasi C++ version locked to 17.

I am probably missing something really obvious, but I expected the code below to compile.

#include <include/tl/expected.hpp>

class Parent
{
private:
    int n =42;
};


class Child
{
public:
    Child() = delete;

private:
    Parent* myParent;

    explicit Child(Parent *p = nullptr)
    : myParent { p }
    {};
};



int main()
{
    // works
    tl::expected<int, std::string> AnswerToUniverse{42};
    
    
    Parent parent;
    Parent* ptr{ &parent };
    tl::expected<Child, std::string> child{parent};
    
    return 0;
}

Instead I get

/TLExpected/main.cpp:33: error: no matching function for call to ‘tl::expected<Child, std::__cxx11::basic_string<char> >::expected(<brace-enclosed initializer list>)’

/TLExpected/build/Desktop-Debug/_deps/tl-expected-src/include/tl/expected.hpp:233:
 error: no type named ‘type’ in ‘struct std::enable_if<false, void>’

Probably not relevant but I am pulling in tl::expected using CPM in the project cmakefile
VERSION 1.1.0
GIT_REPOSITORY "https://github.com/TartanLlama/expected"

@ArchNemSyS
Copy link
Author

ArchNemSyS commented Feb 5, 2025

This works as per stack-overflow question, not only do I not understand it but it feels unnecessary for a movable type

#include <string>
#include <include/tl/expected.hpp>

class Parent
{
private:
    int n =42;
};


class Child
{
public:
    Child() = delete;

    static auto create(Parent *p) noexcept -> tl::expected<Child, std::string> {
        return tl::expected<Parent *, std::string>{p}.transform(
            [](Parent *p) { return Child{p}; });
    }

private:
    Parent* myParent;

    explicit Child(Parent *p = nullptr)
    : myParent { p }
    {};


};



int main()
{
    //works
    tl::expected<int, std::string> AnswerToUniverse{42};

    Parent parent;
    Parent* ptr{ &parent };

    //tl::expected<Child, std::string> child{ptr};

    tl::expected<Child, std::string> child = Child::create(ptr);
    return 0;
}

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