Hot answers tagged

14

The book is talking about what is commonly known as bitfields, and their use is often more memory efficient on most platforms, and especially so in serialization or communication contexts. A boolean requires its own address to be usable by the compiler. This means that while, practically, we only need one bit to represent a true (1) or false (0) condition, ...


12

First, I feel obliged to point out that std::exception and its children were designed a long time ago. There are a number of parts that would probably (almost certainly) be different if they were being designed today. Don't get me wrong: there are parts of the design that have worked out pretty well, and are pretty good examples of how to design an ...


11

Because processors have operations specifically for ints, and specifically for floats. The compiler has to know what operation to target. And I mean, even if you had adding for a series of bits, adding 0110 and 0001 have very different meanings if 0001 is treated as a float rather than an int - not to mention actual operations required to calculate it. And ...


8

The std::bitset string-based constructor only exists since C++11, so it should have been designed with idiomatic use of exceptions in mind. On the other hand I've had people tell me logic_error should basically not be used at all. You may not believe this, but, well, different C++ coders disagree. That's why the FAQ says one thing but the standard ...


8

In 1963, Tony Hoare proposed adding implicit type rules to ALGOL. The ALGOL committee boxed his ears, HARD. Requiring variables to be declared explicitly was known, EVEN THEN, shown to reduce errors in programming. Tony mentioned this in his Turing talk, and said it was BEFORE the probably-apocryphal Venus probe FORTRAN story, where a typo in a FORTRAN DO-...


8

So why did all those programming languages decided to provide multiple built-in data types Because the built-in types like int, float, byte and char are used in almost all use cases – and it turns out that standards are a convenience for everyone. If everyone used their own variant, writing code that uses libraries with different types, exchanging data ...


8

Let's forget for the moment that processors have specific hardware for manipulating byte sequences of a particular size. Let's forget for the moment that processors have specific hardware for operating on specific interpretations of byte sequences (floating-point registers, SEE registers, etc). What good does this abstraction do from a user perspective? ...


7

If you want a TrajectoryPrinter ask for a TrajectoryPrinter. Right now you're only asking for doubles. Something will need to build Oscillator. Something will need to build TrajectoryPrinter. I don't recommend that Oscillator build or even find TrajectoryPrinter. Oscillator shouldn't know TrajectoryPrinter as anything except as something it can call a ...


6

The scheduling mechanism you have described is Fixed-priority pre-emptive scheduling. If you know there is a possibility the max priority queue is always full, then you are using the wrong mechanism, because of starvation as you described. You could prevent starvation by using a different scheduler. For instance, you can say that you process at most f(...


5

Say I have some kind of game where there is a class representing the game world and a class to represent the units. If the game world class has a method to get the unit at a location, that's all well and good. I guess you mean something like this // Gamworld.hpp #ifndef GAMEWORLD_HPP #define GAMEWORLD_HPP #endif #include "Unit.hpp" class Gameworld { ...


5

In C++ you have full flexibility how you want to organize your files. But you have to get accustomed to this freedom to make the good choices: A first practice is to have include guards in headers, in order to avoid that due to shared dependencies, the same header gets included multiple times. A second is discipline: make headers self-sufficient and ...


5

Your options for resolving circular dependencies are: forward declaration Yes, this really is commonly used and isn't considered a hack. Note that only the public interfaces of your two interdependent types need the forward declaration: if the implementation is out-of-line, it's a separate translation unit, and there's no problem with each including the ...


5

What you're talking about will not be directly possible in C++ until we get some form of both reflection introspection and reflection-based generation of types. So you're going to have to wait for a while. The closest you can get right now is to employ inheritance: template<typename T1, typename T2> struct joined : public T1, public T2 { }; Of ...


5

Allowing optional types where they used to be required can make the syntax ambiguous. It is easier to add a keyword (or to reuse one in this case) than changing the grammar.


5

I don't believe such a possibility was ever considered by the C++ committee. Further, although FORTRAN did have a sort-of implicit type system (variables starting with 'I' through 'N' were integers, everything else was real) I can't quite see how such a thing would work in a block-structured language like C++. For example, consider code like this: int f() {...


5

In most cases, there won't be a noticeable difference (unless you have the obvious case with method parameters named the same as member variables). But beware of templates! template <typename T> struct Base { int i; }; template <typename T> struct Derived : public Base<T> { int get_i() { return i; } }; This will cause a ...


5

Short circuit evaluation isn't about true or false. It's about not evaluating part of an expression if you can predict the result without it. Since false && whatever() returns false regardless of whatever() you don't need to call whatever(). Your code doesn't demonstrate this at all. You simply can't demonstrate this with true and false. You ...


4

The C++ syntax is designed in such a way that a declaration always needs a type to distinguish it from assignment. You simply cannot leave out the type, because that already has a different meaning: int i = 1; // Leaving out the type turns it into assignment: i = 1; int i; // Leaving out the type turns it into evaluation: i; You simply need an explicit ...


4

try this... int i = 0; // don't use != You could end up with an infinite loop if something ever steps over i=10 for (; i < 10; i++) { // do stuff } int x = i;


4

Yes, apparently there is such an option. I strongly suggest you avoid using it. Fix the code instead. In early versions of C++, the scope of a variable defined in a for loop header extended to the end of the block containing the loop, making the code in your question valid. In modern versions of the language, the scope ends at the end of the loop, making ...


3

Basically you're making priority a function of two variables, like pricePaid*A + waitingTime*B. That's a perfectly sensible strategy. Suppose you're selling tickets, and the price people pay puts them into one of the queues - high, medium, low. You could look at that as a single ordered priority queue, where the priority is high, medium, or low. Within each ...


3

The easy solution would be one namespace per developer, and I keep track of which names are used. However, I would prefer something more built-to-last. In what way is your suggested solution not "built-to-last"? The thing about namespaces is that it is very easy to move things from one namespace to another. Boost does this all the time. The primary ...


3

The accepted answer explains this for virtual private functions, but that only answers one specific facet of the question, which is considerably more limited than what the OP asked. So, we need to rephrase: Why are we required to declare non-virtual private functions in headers? Another answer invokes the fact that classes must be declared in one block - ...


3

Why nested classes ? Bjarne Stroustrup explains in "The design and evolution of C++", the origin and rationale behind nested classes: original C++ in 1984 had a single name space (page 5 and 102). the use of nested classes was a compromise between the the concept of a class as a scope and the need of compatibility with C (page 102) it was further ...


3

It's largely a matter of personal preference, although some places may make it a formal coding standard. C itself doesn't care. For my part, if I'm defining multiple functions in a single source file, I will define the called functions before the caller: void foo( void ) { ... } void bar( void ) { ... foo(); ... } int main( void ) { ... ...


3

A major difference between C++ programs and Javascript scripts is that a C++ program typically runs for a much longer time than a Javascript script. A C++ program with a GUI executes continuously while you are working with the program. A Javascript script on the other hand only executes for a short time to respond to an event and then it ends (even if it ...


3

There have been languages that, as you suggest, provide only a single datatype and then have operations where the expected encoding of the data is provided as part of the operation, rather than being determined by the type of the variable as it is in most modern languages. The best known of these was probably B, the predecessor of C (in fact, C can be ...


3

What is the difference between a normal int and a 32-bit int? As David Arno points out, you haven't said what you mean by a "normal" int. So is what he saying is when you use bitwise operators in C++ it would only cost 1 bit of memory? No, he's saying that if you want to treat integers as collections of bits, you can do so with bitwise operators. ...


3

Yes. The Windows NT kernel API (which is traditionally accessed by using the functions defined in ntdll.dll) can be accessed directly by use of the int 2e instruction. However this is not a supported way of using the system, and details of the implementation (including function codes) are likely to change between Windows versions. The basic approach is: ...


2

The first option is probably faster. With the second one you have, at least, the additional memory allocation task. Anyway this is a good example of premature optimization: performance shouldn't be a concern for this kind of operation. The snake eats a fruit just occasionally. Between two meals there are a lot of other (complex) events that take place and ...



Only top voted, non community-wiki answers of a minimum length are eligible