Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

New answers tagged

0

Couple of things I found myself so far. Performance wise Huge mistake, to use std::async for multiprocessing, while testing it on my local machine with only one CPU it worked okay, using all the cores, but when switched to test server with more than one CPU, I failed to use all of them (actually 32). Changing this std::thread , without any pain btw, did ...


4

Headers and namespaces Don't using namespace, especially a big and growing namespace like std that's not designed for it. There's no need to include <cmath>. On the other hand, there is a clear need for <iostream>, which has been omitted. Structure There's no need for the Power class; it maintains no state. The functions should simply be ...


12

It's great that you provide a test program. Although it doesn't yet test very much, running it under Valgrind uncovers a few wild accesses: ==17803== Invalid write of size 4 ==17803== at 0x109773: Vector::resize(int) (221707.cpp:68) ==17803== by 0x10938A: main (221707.cpp:103) ==17803== Address 0x4d74c94 is 0 bytes after a block of size 20 alloc'd ==...


12

You tagged c++11, but your code is not at all C++11. You have no move constructor and no brace-initializers. You should implement them. Here's a sample implementation using the copy-and-swap idiom. friend void swap(Vector& a, Vector& b) { using std::swap; swap(a.capacity, b.capacity); swap(a.sizet, b.sizet); swap(a.arr, b.arr); } ...


4

Here are some ideas that may help you improve your code. Use functions Right now this code is all one big block of code in main. This makes it more difficult to read and understand than if functions were used. Simplify your code Consider this sequence of code to read two integers: std::string first; std::getline(std::cin, first); std::istringstream iss(...


4

Adding onto @Chipster's answer, I'd like to suggest some improvements for the Deck class: class Deck { private: std::array<Card, 52> m_card; int m_cardIndex; void swapCard(Card &a, Card &b); inline Card* dealCard(); public: std::vector<Card*> m_playerHand; std::vector<Card*> m_dealerHand; ... ...


4

I never programmed in my life, and I am currently self teaching myself some C++ by reading books, online free classes and googling. If that is true, then I applaud you. This code is better than some code I've seen from people who "know what they're doing". For one thing, I understood it and could tell what you were trying to do. Of course, you can still ...


5

General points: We need to include <functional> for std::greater<>. Don't do using namespace std;. The gfg class has no state, so we can use a free function for satisfiable. Variables should be declared as close as possible to their point of use. If possible they should also be initialized to a valid value, and not a placeholder "invalid value". ...


5

In C++ &/* belongs with type. E.g. void swapCard(Card& a, Card& b); The CTOR member init list becomes easier to read (IMO) when formatted like this: Deck() : m_cardIndex {0} , m_playerHand {} , m_dealerHand {} You can and should use enum class over the "normal" one. (Taken from here): What is the difference between two? ...


5

Moving to an answer: My knowledge on compilers is 20 years old. So I could be wrong. But a compiler can do a lot of the maths on const values (and now contexpr) at compile time (which is when all template code is also evaluated). I have seen compilers check to see if this was a division by 2 (or multiple of 2) and do shifts (if the shifts are actually ...


6

The code is missing #include <type_traits>; it would have been nice to have had the test cases too. It would be nice to be able to check the argument restriction using assert. Unfortunately, this doesn't fit nicely with C++11 constexpr functions, which may contain only a single return statement. If you might also compile against a newer standard, we ...


3

You say you want to target c++17 primarily, but also be backward compatible down to c++11. Consider changing this to target c++11 primarily, but also be compatible with c++17 Because you can't really use most of the c++17 stuff here. You have to write code in c++11, but also take care of c++17 compatibility. To be honest, I don't think this is a good ...


3

Instead of a char array you can use std::aligned_storage. You aren't consistent with your macros you have both constexpr and _CONSTEXPR17. Pick one and stick to it. destroy_at is standard only from C++17 onward. The equivalent syntax is location->~_Ty(); However it may be worth to put this and the placement new into a helper function to move the ugly ...


1

Is there any reason for your users to access your internal buffer? To me that data methods shouldn't be there. Don't like very much those reinterpret_cast but I see you have those there to support not default constructable types. I could live with those and get back that additional feature as long as that implementation detail is sealed into the class.


Top 50 recent answers are included