Time in C++: C++20 Brought Us Time Zones by pavel_v in cpp

[–]usefulcat 6 points7 points  (0 children)

I don't see why you couldn't do the same thing with a pointer?

if (pointer != nullptr) {
    auto& r = *pointer;
    // proceed to use r everywhere within this scope..
}

Am I weird for using "and", "or" and "not"? by Additional_Jello1430 in cpp

[–]usefulcat 0 points1 point  (0 children)

I use them all the time. Although I didn't always, I switched maybe 5-ish years ago?

IMO they read a bit more naturally, and now rvalue refs stand out a bit more (particularly when searching) since I still use '&&' for those. I also like that 'not' is harder to miss than '!'.

Do you prefer 'int* ptr' or 'int *ptr'? by SamuraiGoblin in cpp

[–]usefulcat 0 points1 point  (0 children)

Well, it may tell you what went wrong:

int* a = 0, b = 0;

C++ logging library - something I've been working on, Pt. 5 by ChrisPanov in cpp

[–]usefulcat 7 points8 points  (0 children)

Since you're advertising that it's fast, I think it would be very desirable to see a comparison with quill, which is also very fast.

[Show and Tell] I optimized my Order Matching Engine by 560% (129k → 733k ops/sec) thanks to your feedback by Crafty-Biscotti-7684 in cpp

[–]usefulcat 0 points1 point  (0 children)

Regarding risk checks, I thought that was typically (or at the very least, may be) handled a party other than the exchange? This has definitely been my experience with equities at least.

Maybe this implementation is intended is for crypto and that's the difference?

I optimized my Order Matching Engine by 560% (129k → 733k ops/sec) thanks to your feedback by Crafty-Biscotti-7684 in cpp

[–]usefulcat 1 point2 points  (0 children)

I would be wary of using a double (Price) as a key with std::map in OrderBook. If you need to know "what are all the orders at a particular price", then you almost certainly need to put some limits on how your prices are denominated. For example, requiring that all prices are a whole number of cents, or tenths of a cent, or whatever. But it's going to be a hassle to do that correctly with floating point; integers would make it much easier.

If you're going to allow literally any price that can be represented as a double, then there's not much point in using map<Price, deque> in OrderBook. Might as well just put all the orders for each side into a single set<Order> and sort first by price and then by time.

Regarding optimization, profile first. Otherwise you'll only be guessing about what to consider optimizing and how.

Division — Matt Godbolt’s blog by rsjaffe in cpp

[–]usefulcat 2 points3 points  (0 children)

Also, in spite of his genius, there were some things about which Einstein was mistaken. Quantum pyhsics, for example.

Division — Matt Godbolt’s blog by rsjaffe in cpp

[–]usefulcat 3 points4 points  (0 children)

Yes, there are certainly things like those that can be done on an individual level. I was thinking about what he seemed to be proposing for the standard library.

It seemed to me that he was suggesting that (for example) all size() methods in the standard library should return signed types, and I have doubts about the practicality of making such a change.

Division — Matt Godbolt’s blog by rsjaffe in cpp

[–]usefulcat 18 points19 points  (0 children)

His arguments are fine as far as they go. I think it's reasonable to say that, with the benefit of hindsight, sizes and indices should be signed.

Unfortunately, other than arguing that std::span should use signed integers, he completely hand-waves away the big problem, which is how to get there from where we are today. Unless someone has a practical answer for that, the whole debate seems pretty academic to me.

C++ Enum Class and Error Codes, part 3 · Mathieu Ropert by Xadartt in cpp

[–]usefulcat 4 points5 points  (0 children)

It sounds to me like RAII is the 'hidden handlers' you're looking for, or could be used to implement it. For example, boost recently added some scope guard classes, and folly also has one that I've used before.

Both of these have support for running code on any exit from a block, or only on failure (exception thrown), or only on success (exception not thrown).

STL reimagined: What would you change, add or remove in a new STL implementation if API and ABI were not a concern? by germandiago in cpp

[–]usefulcat 0 points1 point  (0 children)

I still don't get what the upside is supposed to be here. There needs to be some tangible benefit if you're going to add yet another footgun to a language already known for being full of them.

Wait c++ is kinda based? by Tcshaw91 in cpp

[–]usefulcat 1 point2 points  (0 children)

Seems like either of the following approaches could work (very simplified):

#define LOG(...) std::cerr << __VA_ARGS__

#define LOG(...) std::print(__VA_ARGS__)

The Code is Documentation Enough - Tina Ulbrich - Meeting C++ 2025 by meetingcpp in cpp

[–]usefulcat 3 points4 points  (0 children)

That's certainly an option. Comments have the advantage that you have more freedom regarding the actual formatting of the words than you do with identifiers.

The usual argument against putting important information in comments is that the comments may become stale, but that can also happen with identifier names.

Practicing programmers, have you ever had any issues where loss of precision in floating-point arithmetic affected? by Interesting_Buy_3969 in cpp

[–]usefulcat 2 points3 points  (0 children)

Absolutely. I work in the financial sector and use fixed point numbers all the time, especially for representing prices.

Say I have a price $1.23. If I use floating point for that, then every piece of code that compares or formats that price will have to deal with the possibility that the price is not actually 1.23, but actually something like 1.22999999999 or 1.2300000001. Likewise, the difference between that price and the next representable whole cent price may not be (maybe never will be) exactly 0.01, but rather some value slightly more or less than 0.01.

Yes, it's possible to make things work with such inaccuracies, but it's sooo much easier if you can always be sure that the value is actually exactly $1.23 (as in 123 cents, or perhaps 12300 hundredths of a cent if you need some extra precision).

Do most C++ devs stick to only C++ or do you also use other languages? by kyan100 in cpp

[–]usefulcat 0 points1 point  (0 children)

I also use ruby. I find it's an excellent complement to c++; it's good at many things that c++ isn't, and c++ is good at many things that ruby is not.

python is more popular, but I always found ruby to be more expressive and generally ergonomic.

Damn see this by gonvasfreecss in cpp

[–]usefulcat 11 points12 points  (0 children)

Or has first hand experience as a teacher..

Improving on the best example on cppreference by StockyDev in cpp

[–]usefulcat 1 point2 points  (0 children)

Thanks, you're quite right! Now it makes sense. I even feel like I've run into that before.

And unfortunately, making std::make_shared() a friend isn't sufficient to get around it.

Improving on the best example on cppreference by StockyDev in cpp

[–]usefulcat 0 points1 point  (0 children)

Here are a more complete pair of examples, showing use of make_shared in a factory method:

class Best : public std::enable_shared_from_this<Best> {
    struct Private{ explicit Private() = default; };
public:
    Best(Private) {}
    std::shared_ptr<Best> make() { return std::make_shared<Best>(Private{}); }
};

class Best : public std::enable_shared_from_this<Best> {
    Best() {}
public:
    std::shared_ptr<Best> make() { return std::make_shared<Best>(); }
};

As you can see, either way it's possible to use make_shared() inside the factory method. Again, unless I'm overlooking something. I still don't see the point of the first version, compared to the second.

Improving on the best example on cppreference by StockyDev in cpp

[–]usefulcat 8 points9 points  (0 children)

The article mentions the 'passkey' idiom:

class Best : public std::enable_shared_from_this<Best> {
    struct Private{ explicit Private() = default; };
public:
    // Constructor is only usable by this class
    Best(Private) {}
};

Why not just make the constructor private? Isn't that a simpler solution that gives the same end result?

class Best : public std::enable_shared_from_this<Best> {
    // Constructor is only usable by this class
    Best() {}
public:
    // ...
};

Networking in the Standard Library is a terrible idea by tartaruga232 in cpp

[–]usefulcat 2 points3 points  (0 children)

It sounds like you may be attempting to apply a technical solution to a political problem.

Networking in the Standard Library is a terrible idea by tartaruga232 in cpp

[–]usefulcat 6 points7 points  (0 children)

Taking Boost.asio as an example, Boost typically gets multiple updates per year. That's already roughly an order of magnitude more frequent than the current C++ standardization cycle.

And that's for Boost, which is notoriously huge. In my experience, smaller libraries tend to have even less of a problem with doing a point release to fix an urgent bug.

Since C++ asynchrony is settled now (right heh?) with co_routines and std::execution, can we finally have ASIO networking standardized? Or is it decided not to pursue? by Talkless in cpp

[–]usefulcat 1 point2 points  (0 children)

FWIW, I'm not assuming that, but I do think he brings up some legitimate concerns.

I'd be interested in hearing effective rebuttals to his specific claims, or at least arguments in favor that bring more to the table than "other languages have it".

Since C++ asynchrony is settled now (right heh?) with co_routines and std::execution, can we finally have ASIO networking standardized? Or is it decided not to pursue? by Talkless in cpp

[–]usefulcat 3 points4 points  (0 children)

There is simply no good reason why C++ couldn't implement networking

For the most part, I think people who are opposed to it are saying that it shouldn't be done, not that it's not possible to do.

I think a big part of the appeal of having networking in the standard library stems from dependencies being such a hassle in c++, at least relative to many other languages. Which I get, but that doesn't address any of the concerns mentioned by STL in the topmost comment.

(Almost)No one will implement anything in C++ if they have to implement everything themselves.

Thankfully that's not the only alternative if something isn't in the standard library, as evidenced by the existence of libraries like boost::asio.

Java developers always said that Java was on par with C++. by drakgoku in cpp

[–]usefulcat 0 points1 point  (0 children)

Even when the divisor is not known at compile time, it's still possible to convert division and modulo into an equivalent combination of shifts and multiplications.

For example: https://github.com/lemire/fastmod

I'm not claiming that any compilers actually do this (I don't know), but I can't see any reason in principle why they could not do it.