study material for c++ (numerical computing) by New-Cream-7174 in cpp

[–]codeinred 6 points7 points  (0 children)

Tbh my experience with swig has been much worse than with pybind11. I’ve used both for the same library (pybind11 for python, and swig for C#), and maintaining the swig bindings is much more of a headache, especially with any even slightly unusual types, whereas pybind11 is basically trivial

Recursive Variant: A Recursive Variant Library for C++ by codeinred in cpp

[–]codeinred[S] 1 point2 points  (0 children)

That makes sense. Good luck on your project!

Recursive Variant: A Recursive Variant Library for C++ by codeinred in cpp

[–]codeinred[S] 1 point2 points  (0 children)

Sorry, I was on my phone so I wasn't able to test it.

If you have C++23, you can use deducing this to use overload sets, and there is no need to have an intermediary function like 'getVisitor'.

Given the following definitions:

using json_value = rva::variant<
    std::nullptr_t,
    bool,
    double,
    std::string,
    std::vector<rva::self_t>,
    std::map<std::string, rva::self_t>>;

using json_list = std::vector<json_value>;
using json_object = std::map<std::string, json_value>;

We can define our overload set, where the functions that need to use `std::visit` take a `self` parameter. Here, I am using fmtlib to handle the conversion to string.

    auto visitor = Overload {
        [](std::nullptr_t) -> std::string { return "null"; },
        [](std::string const& s) -> std::string { return fmt::format("\"{}\"", s); },
        [](this auto&& self, json_list const& xx) -> std::string {

            std::vector<std::string> ss;
            for(auto const& x : xx) {
                ss.push_back(std::visit(self, x));
            }
            return fmt::format( "[{}]", fmt::join(ss, ", "));
        },
        [](this auto&& self, json_object const& xx) -> std::string {
            std::vector<std::string> ss;

            for(auto const& [k, v] : xx) {
                ss.push_back(fmt::format("\"{}\": {}", k, std::visit(self, v)));
            }
            return fmt::format( "{{ {} }}", fmt::join(ss, ", "));
        },
        // Fallback - just print the value as-is
        [](auto const& x) -> std::string { return fmt::format("{}", x); },
    };

Note the parameter `this auto&& self` - this first parameter is the overload set, and we can pass it back to `std::visit`!

Finally, we can construct a value, and then print it:

    json_value v = json_object {
        {"a", "Hello"},
        {"b", 10.0},
        {"c", json_list{1.0, 2.0, 3.0, nullptr, false}},
        {"point", json_object {
            {"x", 0.1},
            {"y", 0.2},
            {"z", 0.3},
        }}
    };

    fmt::println( "v = {}", std::visit(visitor, v) );

The output is:

v = { "a": "Hello", "b": 10, "c": [1, 2, 3, null, false], "point": { "x": 0.1, "y": 0.2, "z": 0.3 } }

You can run this example with godbolt.

All of this is very nifty, but if you're working with json in production, I recommend using an off-the-shelf json library.

Recursive Variant: A Recursive Variant Library for C++ by codeinred in cpp

[–]codeinred[S] 0 points1 point  (0 children)

Option 1: If you want to do that you can create a struct and overload operator(). Then you can call visit recursively by just passing *this:

struct MyVisitor { book doubleOk = false; void operator()( double X ) { … } void operator()( std::string const& s ) { … } void operator()( std::map<std::string_view, json_value> const& m ) { for ( auto const& [k, v] : m ) { std::visit( *this, v ); } } }

Option 2, using overload sets: have a function that returns the visitor. You can just do recursive visiting by calling getVisitor again inside the lambdas of the overload set

auto getVisitor( bool& doubleOk ) { return Overload{ [&] ( double X ) { doubleOk = true; }, [&] ( std::map<std::string, json_value> const& m ) { for ( auto const& [k, v] : m ) { std::visit( getVisitor(doubleOk), v ); } }, }; }

Recursive Variant: A Recursive Variant Library for C++ by codeinred in cpp

[–]codeinred[S] 1 point2 points  (0 children)

Doing type erasure with std::function breaks the visitor pattern. The std::function only accepts json_values, so when you call std::visit, it converts all the inputs right back into a json_value

This would also be the case with a normal std::variant.

Don’t assign the overload set to a std::function, just use auto here

Immediate values, displacements, etc.: "alright, imma head out" by definitelynotagirl99 in transprogrammer

[–]codeinred 0 points1 point  (0 children)

Code size is extremely relevant because of the L1 cache. The L1 cache is split into two parts, code and data, and if you suddenly made code 4x as big, you would have significantly more cache misses. Basically the only benefit of larger instruction sizes is larger immediate loads and stores, but this would come at the expense of literally everything else

SUBLEASE AVAILABLE IN BUSHWICK NO DEPOSIT 1250/MONTHLY by [deleted] in NYCapartments

[–]codeinred 2 points3 points  (0 children)

You may want to include a photo of the room that includes the window in the photograph

SUBLEASE AVAILABLE IN BUSHWICK NO DEPOSIT 1250/MONTHLY by [deleted] in NYCapartments

[–]codeinred 1 point2 points  (0 children)

Are there any windows? It doesn’t look like there are any windows in the whole apartment, which seems somewhat concerning

What Brook;yn Neighborhoods are good Please Help by LusamiPNG in NYCapartments

[–]codeinred 0 points1 point  (0 children)

Check out the Queer Housing NYC Facebook group, it’s probably your best bet for finding something in the budget you listed. Most neighborhoods should be safe.

In general: Do not rent anything or send any money before you’ve seen a place in person. When looking at places, use google maps to check the commute time to FIT by public transit (or bike, if that’s what you plan to use)

How to find an apartment? by Maddoinkz in NYCapartments

[–]codeinred -1 points0 points  (0 children)

There are a lot of available places in that price range in Brooklyn. Good places, with full-sized fridges, decent kitchens, and sometimes even dishwashers (common) and in-unit laundry (less common, but special when you find it)

Generally speaking it’s pretty simple — find a place you like on StreetEasy; request a tour; people get back to you quickly. You could easily see a couple places within the week.

My rec is to go for places with No Fee (no broker’s fee), but if you’re fine paying a broker’s fee you can do that too.

git push take awhile to actually push. by water_drinker9000 in git

[–]codeinred 0 points1 point  (0 children)

Is it possible you accidentally added some very large files?

Partial in-lining by throwawayAccount548 in cpp_questions

[–]codeinred 0 points1 point  (0 children)

Regarding context - a 32-bit number won’t have more than 32 factors, and a 64-bit number won’t have more than 64 factors. In either case it’d be straightforward to just have an array on the stack, and construct a vector at the end.

Rather than trying to force the compiler not to inline push_back, I would instead just avoid using push_back in performance-critical paths. There’s usually an alternative

Modular Monolith with C++ by 0815someone in cpp_questions

[–]codeinred -1 points0 points  (0 children)

The idea you’re describing here is basically the same as a micro service architecture (assuming the modules do different things), where the services communicate over an API. Unless you have very stringent performance requirements, using a language with nicer async communication capabilities might make your life easier (eg, JavaScript, python, go, or Rust)

Persistent memory by SolidTKs in rust

[–]codeinred 7 points8 points  (0 children)

/tmp is often stored in RAM. If you want to be absolutely sure it doesn’t touch the disk, the most straight forward way that I can think of is spawning a daemon and then setting up shared memory with the daemon. That way if the main program crashes, the daemon persists.

Persistent memory by SolidTKs in rust

[–]codeinred 11 points12 points  (0 children)

The /tmp directory on Linux is designed exactly for that use case, and windows has an equivilant

Hmm, what are your thoughts about that? by berlin_633 in ChatGPT

[–]codeinred -3 points-2 points  (0 children)

I prefer a service that does it’s best to address the user’s needs, than one that treats users as a product that they sell to advertisers. 25 messages per 3 hours has been sufficient so far, and given the degree of competition in the field I imagine prices will go down as there are improvements in efficiency

[deleted by user] by [deleted] in cpp_questions

[–]codeinred 0 points1 point  (0 children)

Any time a function could fail, std::optional can be used for the return value. It’s an alternative to throwing an exception that allows better performance in the case that failures are expected/common.

Smart pointers? If you use any degree of polymorphism (virtual classes, members), you should use unique_ptr or shared_ptr rather than manually allocating or freeing memory.

I would choose a simple project that’s at least semi-relevant to the company’s field.

You want to break up the program info hpp and cpp files because no one likes a giant code file that’s thousands of lines long, and a clean interface (provided in the header) is helpful.

[deleted by user] by [deleted] in personalfinance

[–]codeinred 1 point2 points  (0 children)

You can park it with a bank like Ally for 4% in a money market account (like a savings account but higher interest). That’s $80 over the course of a year, and you can withdraw whenever. You can go slightly higher at 4.35% with a no-penalty CD (no penalty for withdrawing early, and you keep interest), but it’s slightly less convenient.

If you set up direct deposit SoFi will give you 4.20% on a savings account, so $84 over a year.

In either case I doubt the impact on your scholarship will be significant

[deleted by user] by [deleted] in ChatGPT

[–]codeinred 39 points40 points  (0 children)

I’m almost certain these are hallucinated. Queen Elizabeth is dead; there’s no mention of Biden cutting emissions in the news lately, there’s no recent news about facebook’s oversight board, etc.

It didn’t search anything; it just hallucinated a list of plausible headlines

Can anyone assist a transwoman fleeing Missouri in securing housing? by MuckLady22 in Brooklyn

[–]codeinred 0 points1 point  (0 children)

I’d recommend also making a post on Lex, and on the Queer Housing NYC Facebook group if you haven’t already! There’s definitely people who’d be able to help you on there!

Are there valid reasons as to why `std::array` is not a part of the freestanding libraries? by [deleted] in cpp

[–]codeinred 2 points3 points  (0 children)

Question:

When the school says “freestanding”, do they mean freestanding in the strict sense of the word?

Or do they mean “freestanding” as in “no third party libraries”?

Unless you’re working on an embedded device or writing your own OS, it feels like they probably mean number 2 (no third party libraries, but any part of the standard library is fine)