c++

How do I read binary C++ protobuf data using Python protobuf?

The Python version of Google protobuf gives us only: SerializeAsString() Where as the C++ version gives us both: SerializeToArray(...) SerializeAsString() We're writing to our C++ file in binary format, and we'd like to keep it this way. That said, is there a way of reading the binary data into Python and parsing it as if it were a...

A C++ library for Arrays, Matrix, Vector, and classical linear algebra operations

Which library do you use for N-dimensional arrays? I use blitz++ at work and I really dislike some aspect of it. Some aspect of it are even dangerous. The need for resizing before using operator=. A(Range::all(), Range::all()) throws for an (0,0) matrix, etc. and the linear algebra operations are to be done via clapack. I used and lov...

Why is `i = ++i + 1` unspecified behavior?

Consider the following C++ Standard ISO/IEC 14882:2003(E) citation (section 5, paragraph 4): Except where noted, the order of evaluation of operands of individual operators and subexpressions of individual expressions, and the order in which side effects take place, is unspecified. 53) Between the previous and next seque...

Code with undefined behavior in C#

In C++ there are a lot of ways that you can write code that compiles, but yields undefined behavior (Wikipedia). Is there something similar in C#? Can we write code in C# that compiles, but has undefined behavior? ...

On declarative programming in C++

Often I face the problem of mapping the parameter space of one API onto the parameter space of another one. Often I see this solved by nested nested nested ... switch statements. And I was wondering if there would happen to be a library or a technique that allows you to 'declare' the mapping instead of 'program' it. A trivial example ...

strange double to int conversion behavior in c++

The following program shows the weird double to int conversion behavior I'm seeing in c++: #include <stdlib.h> #include <stdio.h> int main() { double d = 33222.221; printf("d = %9.9g\n",d); d *= 1000; int i = (int)d; printf("d = %9.9g | i = %d\n",d,i); return 0; } When I compile and run the program, I see: g++ test.cp...

Your thoughts on "Large Scale C++ Software Design"

Reading the reviews at Amazon and ACCU suggests that John Lakos' book, Large-Scale C++ Software Design may be the Rosetta Stone for modularization. At the same time, the book seems to be really rare: not many have ever read it, and no pirate electronic copies are floating around. So, what do you think? ...

What can you use to cast an int/float to a wchar_t*?

If you have an int or float, how can you cast it to a wchar_t* without using external libraries like boost? ...

Why does 'unspecified_bool' for classes which have intrinsic conversions to their wrappered type fail?

I have recently read the safe bool idiom article. I had seen this technique used a few times, but had never understood quite why it works, or exactly why it was necessary (probably like many, I get the gist of it: simply using operator bool () const allowed some implicit type conversion shenanigans, but the details were for me always a ...

convert integer to array, c++

I would like to convert an integer into an array, so that it looks like the following: int number = 123456 ; int array[7] ; with the result: array[0] = 1 array[1] = 2 ... array[6] = 6 ...

Exception handling

I heard people say exception handling is a bit expensive because of the stack unwinding. I don't get something, the stack unwinding happens whether I throw an exception and whether I use "return". So where is the difference? If for example I get a memory problem that I can't handle - the only option is to stop the function till the I r...

How to initialize static const pointer in a class ?

class School { static const int *classcapacity; }; This expression is from my exam and it need to get initialized how can i do that ? ...

not2 stl negator

Hi, Studying STL I have tried to negate a functor with not2 but encontered problems. Here is the example: #include <iostream> #include <vector> #include <functional> #include <algorithm> using namespace std; struct mystruct : binary_function<int,int,bool> { bool operator() (int i,int j) { return i<j; } }; template <class T> class gen...

How to Calculate Execution Time of a Code Snippet in C++

Hi folks. I should compute execution time of a C++ code snippet in seconds. It must be working either on Windows or Unix machines I use code the following code to do this. (import before) clock_t startTime = clock(); // some code here // to compute its execution duration in runtime cout << double( clock() - startTime ) / (double)CLO...

Prevent Modal Dialog on win32 process crash

We have a legacy build infrastructure for nightly builds (implemented in Perl) to compile, link and unit tests our applications/plugins. On Windows, if the unit testing process crashes, this pops up a Modal Dialog which "locks" our build farm. Is there a way (win32 API call, system config, env var, something...) to disable this behavior...

[C++] Array of char or std::string for a public library?

Hi, my question is simple: Should I use array of char eg: char *buf, buf2[MAX_STRING_LENGTH] etc or should I use std::string in a library that will be used by other programmers where they can use it on any SO and compiler of their choice? Considering performance and portability... from my point of view, std strings are eas...

.c_str() weirdness? Data changes without rhyme or reason?

I have this simple function: const wchar_t *StringManager::GetWCharTStar(int stringId) { std::wstring originalString = StringManager::GetString(stringId); const wchar_t *retStr = originalString.c_str(); return retStr; } At the second line of that function, I have the correct wchar_t*. However, when I go to return, the dat...

Does defining a function inside a header always make the compiler treat it as inline?

I just learned that defining a c++ function inside a class's header file make the function inline. But I know that putting the inline keyword next to a function is only a suggestion and the compiler wont necessarily follow it. Is this the same for header defined c++ functions and is there a difference in behavior between a standalone ...

Is there a way to prevent a header defined c++ function from being treated as inlined.

Hi, I am making a Qt application and as I was coding, I took the habit of defining my slots in the header. I found it was easier for me to develop that way though I still define normal functions in the .cpp (unless the function is really small). But now there are some worries from my colleague that putting these in the header is bad p...

Is it possible to define multiple classes in just one .cpp file?

I'm working on a project for school and the instructor insists that all code go into one .cpp file (for easier grading on his part). I would like to define multiple classes within this file. Will I run into any problems in doing this? ...