←back to thread

In Defense of C++

(dayvster.com)
185 points todsacerdoti | 7 comments | | HN request time: 0s | source | bottom
Show context
loeg ◴[] No.45268662[source]
> in C++, you can write perfectly fine code without ever needing to worry about the more complex features of the language. You can write simple, readable, and maintainable code in C++ without ever needing to use templates, operator overloading, or any of the other more advanced features of the language.

This... doesn't really hold water. You have to learn about what the insane move semantics are (and the syntax for move ctors/operators) to do fairly basic things with the language. Overloaded operators like operator*() and operator<<() are widely used in the standard library so you're forced to understand what craziness they're doing under the hood. Basic standard library datatypes like std::vector use templates, so you're debugging template instantiation issues whether you write your own templated code or not.

replies(7): >>45268759 #>>45268766 #>>45269024 #>>45272274 #>>45272736 #>>45274243 #>>45274785 #
AaronAPU ◴[] No.45269024[source]
I’ve been programming C++ on a daily basis for more than 20 years and literally never use the >> operator. Never. Not rarely, never.
replies(3): >>45269514 #>>45270521 #>>45273619 #
pklausler ◴[] No.45269514[source]
How do you shift bits to the right?
replies(2): >>45270143 #>>45270883 #
1. voxelghost ◴[] No.45270883[source]
right_shifted = value / (1 << bits)
replies(2): >>45270993 #>>45271665 #
2. ◴[] No.45270993[source]
3. taneq ◴[] No.45271665[source]
Division is slow, though. You should use something like:

right_shifted = (int)(value * pow(2, -bits) - 0.5)

replies(2): >>45271899 #>>45273196 #
4. jandrewrogers ◴[] No.45271899[source]
Or just rely on the compiler to automatically do trivial conversions. Which are pretty reliable these days.
replies(1): >>45273812 #
5. voxelghost ◴[] No.45273196[source]
I dont trust my intel FPU to accurately representa that ;-)
6. account42 ◴[] No.45273812{3}[source]
I'm pretty sure the post you are responding to is not seriously suggesting using floating point multiplication and exponentiation as a performance optimization ;)
replies(1): >>45275143 #
7. voxelghost ◴[] No.45275143{4}[source]
Offcourse not, that would be silly. Just overload the ^ operator like any normal person.

#include <iostream> #include <bitset>

class Bits { unsigned value; public: explicit Bits(unsigned v) : value(v) {}

    // Shift operator ^ : positive = left, negative = right
    Bits operator^(int shift) const {
        if (shift > 0) {
            return Bits(value << shift);
        } else if (shift < 0) {
            return Bits(value >> -shift);
        } else {
            return *this; // no shift
        }
    }

    friend std::ostream& operator<<(std::ostream& os, const Bits& b) {
        return os << std::bitset<8>(b.value); // print 8 bits
    }
};

int main() { Bits x(0b00001111);

    std::cout << "x       = " << x << "\n";
    std::cout << "x ^ 2   = " << (x ^ 2)  << " (shift left 2)\n";
    std::cout << "x ^ -2  = " << (x ^ -2) << " (shift right 2)\n";
}