c++

Custom Exceptions in C++

Hi, I've been trying to make some custom exception classes for a C++ library I'm working on. These custom exceptions capture extra info, such as file,line number,etc, needed for debugging, if for some reason while testing an exception is not caught in the right place. However most people seem to recommend inheriting from the std::excepti...

boost::unordered_map maintains order of insertion?

I am looking for a container which provides std::map like interface but maintains the order in which elements are inserted. Since there will not be too many elements in the map, the lookup performance is not a big issue. Is boost::unordered_map will work in this case? i.e. does it maintain the order of insertion. I am new to boost librar...

How does compiler choose between template specializations featuring an array?

I just came across std::tr1::extent template and it puzzled me. I never ever dealt with array type parameters in my life so I don't understand how they work. So, given the code from gcc type_traits template<typename _Tp, unsigned _Uint, std::size_t _Size> struct extent<_Tp[_Size], _Uint> template<typename _Tp, unsigned _Uint> ...

Index, assignment and increment in one statement behaves differently in C++ and C#. Why?

Why is this example of code behaving differently in c++ and C#. [C++ Example] int arr[2]; int index = 0; arr[index] = ++index; The result of which will be arr[1] = 1; [C# Example] int[] arr = new int[2]; int index = 0; arr[index] = ++index; The result of which will be arr[0] = 1; I find this very strange. Surely there must be so...

#include directive: diff between "test.h" and "./test.h"

Is there any difference between #include "./test.h" and #include "test.h" for C/C++ preprocessor? ...

PInvoke - how to represent a field from a COM interface

I am referencing a COM structure that starts as follows: [scriptable, uuid(ae9e84b5-3e2d-457e-8fcd-5bbd2a8b832e)] interface nsICacheSession : nsISupports { /** * Expired entries will be doomed or evicted if this attribute is set to * true. If false, expired entries will be returned (useful for offline- * mode and cli...

is this a good way to do a strcmp to return false when strings are empty

I want another condition --still maintaining a fast execution time but safer-- where i return false if either or both strings is empty: int speicial_strcmp(char *str1, char* str2 ) { if(*str1==*str2 =='\0') return 0; return strcmp(str1,str2); } ...

How To: Create a Group Policy Extension (Windows XP/Vista/7)

Hello. I would like to write a Group Policy Extension for my application. The config is rather complex (hierarchical) and so doing it via ADMX is not a likely option. The problem is I cannot find any sample code to help me even get started. The MSDN help (http://msdn.microsoft.com/en-us/library/aa374176%28VS.85%29.aspx) offers little in...

Why does SFINAE not apply to this?

I'm writing some simple point code while trying out Visual Studio 10 (Beta 2), and I've hit this code where I would expect SFINAE to kick in, but it seems not to: template<typename T> struct point { T x, y; point(T x, T y) : x(x), y(y) {} }; template<typename T, typename U> struct op_div { typedef decltype(T() / U()) type; ...

std::pair<int, int> vs struct with two int's

In an ACM example, I had to build a big table for dynamic programming. I had to store two integers in each cell, so I decided to go for a std::pair<int, int>. However, allocating a huge array of them took 1.5 seconds: std::pair<int, int> table[1001][1001]; Afterwards, I have changed this code to struct Cell { int first; int s...

Viewing data in a circular buffer in real-time

I have an incoming stream of messages, and want a window that allows the user to scroll through the messages. This is my current thinking: Incoming messages go into single producer single consumer queue A thread reads them out and places them into a circular buffer with a sequential id This way I could have multiple incoming streams s...

operator new/delete and class hierarchies

hello. suppose,we have hierarchy of classes and we want to make them allocate/deallocate theirs memory only throughout our memory manager. what is a classical C++ way to achieve this behavior? is it a MUST* to have additional checks such as: class Foo{ public: virtual ~Foo(){} void* operator new(size_t bytes) { i...

Qwt log10scaleEngine causes weird filling of the curves

Hello. I'm using qwt chart package and have encountered a problem. I've tried to combine few examples from the web-page into one, and log10scaleEngine behaves strange together with curves that are filled with color. The problem is with switching to log10scale: the curves are filled upwards (instead of downwards): 1) linear scale, 2) lo...

Why is a type qualifier on a return type meaningless?

Say I have this example: char const * const foo( ){ /* which is initialized to const char * const */ return str; } What is the right way to do it to avoid the compiler warning "type qualifier on return type is meaningless"? ...

Qt4 login window

I am writing a login window in Qt. When the users clicks on OK, it should close the login window, show a "Connecting to server..." Widget, and open the main window once the connecttoserver method has done its job. However, the widget appears only when the main window is shown, and disappears immediately (it shouldn't even close!) How...

warning #411: class foo defines no constructor to initialize the following:

I have some legacy code built with c++ compiler giving the error in the subject line typedef struct foo { char const * const str; } Foo; and a lot of places in the code (meaning I cannot change all of them) use it in a C style initialization: Foo arr[] ={ {"death"}, {"turture"}, {"kill"} } What is the good wo...

PInvoke - reading the value of a string field - "Attempted to read or write protected memory"

I'm having trouble accessing the some string fields in a COM interface. Calling the integer fields does not result in an error. When attempting call clientID(), deviceID() or key(), I get the old "Attempted to read or write protected memory" error. Here is the source interface code: (code sourced from here) [scriptable, uuid(fab51c92-9...

How to Identify a Missing Dependency

We have a legacy 3rd party program that is failing with the error "Class Not Registered" when it tries to execute certain functionality. Is there a way to tell what class it's looking for? Sometimes it says "Catastrophic error" instead. Tried Dependency Walker statically and profiling, Kernal32.exe errors. I'm guessing that's the ins...

Why doesn't this while loop work?

Ok, so I'm trying to create a program using a while loop to find the greatest common divisor of two numbers. This is what I came up with. However, from what I can tell, the program just seems to skip the loop entirely when I run it. (opers remains 0, divisor always comes back as equal to num1). Anyone out there that can help out a newbie...

PyQt custom widget in c++

Can I write custom Qt widget in pure C++, compile it and use in PyQt? I'm trying to use the ctypes-opencv with qt and I have performance problems with python's code for displaying opencv's image in Qt form. ...