Giter VIP home page Giter VIP logo

Comments (4)

ANUSHRAV01 avatar ANUSHRAV01 commented on May 26, 2024

@danakj I am trying to figure out myself about this. But I like to help as much as i can :-

Yes, you are correct that if the value being constructed in the choice is trivially-default-constructible, its lifetime can begin by being assigned to (as specified in 11.5.6.1 of the C++20 spec).

As a result, in such cases, it would be possible to provide a constexpr path for Choice<...>::with(y) and Choice<...>::set(y).

from subspace.

ANUSHRAV01 avatar ANUSHRAV01 commented on May 26, 2024

@danakj This is a code snippet which might help you with what u are trying to do
template<typename T, typename... Ts>
struct Choice {
// ...

template<typename X>
static constexpr Choice<T, Ts...> with(X y) {
    if constexpr (std::is_same_v<T, X>) {
        return Choice<T, Ts...>{true, y};
    } else {
        return Choice<Ts...>::template with<X>(y);
    }
}

// ...

};

from subspace.

ANUSHRAV01 avatar ANUSHRAV01 commented on May 26, 2024

In this example, we use if constexpr to conditionally return a Choice object with the value y assigned to it, depending on whether X is the same type as T. This approach allows us to create a constexpr path for Choice<...>::with(y) when y is trivially-default-constructible.

A similar approach could be used for Choice<...>::set(y), with the main difference being that set(y) modifies an existing Choice object rather than creating a new one.

Note that in both cases, it is important to ensure that y is indeed trivially-default-constructible in order for these constexpr paths to be valid.

@danakj If u find this useful. Kindly inform me as this motivates me to contribute more to open-source and help community to grow more. And if i can help more just reach out

from subspace.

danakj avatar danakj commented on May 26, 2024

std::construct_at() allows changing the active union member in a constexpr context without it being trivially default constructible, as a drop-in replacement for placement-new. With that, all of Choice can become constexpr.

from subspace.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.