this post was submitted on 24 Jun 2025
14 points (100.0% liked)

C++

2040 readers
2 users here now

The center for all discussion and news regarding C++.

Rules

founded 2 years ago
MODERATORS
 
top 8 comments
sorted by: hot top controversial new old
[–] bollybing@lemmynsfw.com 3 points 1 week ago (1 children)
[–] nodeluna@programming.dev 1 points 1 week ago

what don't u get it? why did I make this? or what is the point of this type?

if you are unfamiliar with std::expected then check out https://en.cppreference.com/w/cpp/utility/expected.html

it's basically a type that let you return either a "value" or an "error" and the caller of the function has to check which did the function return. it's a modern way of handling errors in C++ that was introduced in C++23

[–] lambalicious@lemmy.sdf.org 3 points 1 week ago (1 children)

Without a much deep look at the code: any particular reason to not target also C++14?

[–] nodeluna@programming.dev 2 points 1 week ago* (last edited 1 week ago) (1 children)

because "if constexpr(...)" is a c++17 feature which i'm using it to allow usage of nl::unexpected() to return a nl::expected<nl::monostate, E> to nl::expected<T, E> in this copy constructor

template<class U>
expected(const expected<U, E>& other) : _has_value(other.has_value())   // a copy constructor  
{
        if (_has_value)
        {
                if constexpr (std::is_same<U, monostate>::value) // it checks if U == monostate
                {
                        // makes an empty instance of "T"
                }
                else if constexpr (std::is_same<U, T>::value) // it checks if U == T
                {
                        // otherwise copies "other._value" into _value
                }
                else
                {
                        static_assert(
                            not std::is_same<U, T>::value, "no available conversion between the provided value types");
                }
        }
        else
        {
                new (std::addressof(_error)) E(other.error());
        }
}

 template<class E>
 expected<monostate, E> unexpected(const E& e) // then this can covert <monostate, E> to <T, E> fine because of this copy constructor
 {                
         return expected<monostate, E>(e);
 }


// example usage

nl::expected<int, std::string> meow = nl::unexpected("error");

but i could take a different approach and make 2 copy constructor one that explicitly takes

expected(const expected<monostate, E>& other)

and another

expected(const expected& other)

I was also using "std::is_same_v" which is a c++17 feature instead "std::is_same<>::value" but i made a commit and changed it. it now compiles with c++14 but with c++17 extensions

[–] lambalicious@lemmy.sdf.org 2 points 1 week ago (1 children)

That if constexpr in the case you mention does make the constructor clean as heck. Thanks for the clarification and the commits, by the way!

[–] nodeluna@programming.dev 2 points 1 week ago (1 children)

ikr, constexpr is pretty cool. sure, no problem. I could make it fully compatible with c++14 without c++17 extensions if u wanna use it with c++14

[–] lambalicious@lemmy.sdf.org 2 points 1 week ago (1 children)

I mean, I am not too concerned and it is your code. The if constexpr is just so much spiffier. You should do what's best for you.

My own use case is that for pre-C++23 (and in particular, pre-C++20) workflows I'm looking to homogeneize the set of dependencies used to supplement the standard library and this looks like a great candidate lib. I already have my own expected, actually, but would rather someone else's who knows what they're doing. Since C++14 is the oldest standard I have to support directly for clients (big improvement: was C++03 before 2021) and it brought in the big changes that made constexpr actually usable, I treat C++14 as a a sorta "C++ LTS" in my codebase.

[–] nodeluna@programming.dev 2 points 6 days ago* (last edited 6 days ago)

alright then.

I see. expected is such a great library to have regardless of the standard version. oh c++03, I'm not familiar with that standard.

I enabled support for c++11 regardless, it's kinda cool to do so