1 . Why does F# have a unary plus operator?

Answer :

I'll summarize the extended comments. Possible reasons (until a more authoritative answer is given):

  1. Consistency with OCaml, from which F# is derived (if you're doing something wrong/unnecessary it's best to keep doing it so people know what to expect :-))
  2. Overloading (mostly for custom types)
  3. Symmetry with unary negation

Leave a Comment

1 . How do you pack a visual studio c++ project for release?

Answer :

Be aware that Microsoft do not recommend that you static link the runtime into your project, as this prevents it from being serviced by windows update to fix critical security bugs. There are also potential problems if you are passing memory between your main .exe and .dll files as if each of these static links the runtime you can end up with malloc/free mismatch problems.

You can include the DLLs with the executable, without compiling them into the .exe and without running the redist tool - this is what I do and it seems to work fine.

The only fly in the ointment is that you need to include the files twice if you're distributing for a wide range of Windows versions - newer OSs need the files in manifest-defined directories, and older ones want all the files in the program directory.

2 . C/C++ library for reading MIDI signals from a USB MIDI device?

Answer :

PortMidi is another open source cross-platform MIDI I/O library worth checking out. On the other hand, if you are working on a sysex type of app, then direct Win32 works easily enough.

Just came across another open source cross-platform framework that includes MIDI support: Juce.

Also, I should note that there isn't anything special about a USB connected MIDI device. It will still be presented as a MIDI device in Windows and you will use standard MIDI APIs (mmsystem) to communicate with it.

[July 2014] I just came across RtMidi that looks to be a nice, compact, open source cross-platform C++ library.

3 . What is the best way to create a sparse array in C++?

Answer :

For C++, a map works well. Several million objects won't be a problem. 10 million items took about 4.4 seconds and about 57 meg on my computer.

My test application is as follows:

#include <stdio.h>
#include <stdlib.h>
#include <map>

class triple {
    int x;
    int y;
    int z;
    bool operator<(const triple &other) const {
        if (x < other.x) return true;
        if (other.x < x) return false;
        if (y < other.y) return true;
        if (other.y < y) return false;
        return z < other.z;

int main(int, char**)
    std::map<triple,int> data;
    triple point;
    int i;

    for (i = 0; i < 10000000; ++i) {
        point.x = rand();
        point.y = rand();
        point.z = rand();
        //printf("%d %d %d %d\n", i, point.x, point.y, point.z);
        data[point] = i;
    return 0;

Now to dynamically choose the number of variables, the easiest solution is to represent index as a string, and then use string as a key for the map. For instance, an item located at [23][55] can be represented via "23,55" string. We can also extend this solution for higher dimensions; such as for three dimensions an arbitrary index will look like "34,45,56". A simple implementation of this technique is as follows:

std::map data<string,int> data;
char ix[100];

sprintf(ix, "%d,%d", x, y); // 2 vars
data[ix] = i;

sprintf(ix, "%d,%d,%d", x, y, z); // 3 vars
data[ix] = i;

4 . Thread safe lazy construction of a singleton in C++?

Answer :

Answer features what's called double-checked locking which isn't supported by the C/C++ memory model. (It is supported by the Java 1.5 and later — and I think .NET — memory model.) This means that between the time when the pObj == NULL check takes place and when the lock (mutex) is acquired, pObj may have already been assigned on another thread. Thread switching happens whenever the OS wants it to, not between "lines" of a program (which have no meaning post-compilation in most languages).

Furthermore, as Matt acknowledges, he uses an int as a lock rather than an OS primitive. Don't do that. Proper locks require the use of memory barrier instructions, potentially cache-line flushes, and so on; use your operating system's primitives for locking. This is especially important because the primitives used can change between the individual CPU lines that your operating system runs on; what works on a CPU Foo might not work on CPU Foo2. Most operating systems either natively support POSIX threads (pthreads) or offer them as a wrapper for the OS threading package, so it's often best to illustrate examples using them.

If your operating system offers appropriate primitives, and if you absolutely need it for performance, instead of doing this type of locking/initialization you can use an atomic compare and swapoperation to initialize a shared global variable. Essentially, what you write will look like this:

MySingleton *MySingleton::GetSingleton() {
    if (pObj == NULL) {
        // create a temporary instance of the singleton
        MySingleton *temp = new MySingleton();
        if (OSAtomicCompareAndSwapPtrBarrier(NULL, temp, &pObj) == false) {
            // if the swap didn't take place, delete the temporary instance
            delete temp;

    return pObj;

This only works if it's safe to create multiple instances of your singleton (one per thread that happens to invoke GetSingleton() simultaneously), and then throw extras away. The OSAtomicCompareAndSwapPtrBarrier function provided on Mac OS X — most operating systems provide a similar primitive — checks whether pObj is NULL and only actually sets it to temp to it if it is. This uses hardware support to really, literally only perform the swap once and tell whether it happened.

Another facility to leverage if your OS offers it that's in between these two extremes is pthread_once. This lets you set up a function that's run only once - basically by doing all of the locking/barrier/etc. trickery for you - no matter how many times it's invoked or on how many threads it's invoked.

5 . C++ object instance stays on list after being destroyed

Answer :

You need to clean up the links to this in your followers and following when an object is destroyed.

Something like

    std::cout << "Destructor ID: " << id << std::endl;
    if (!isCopy)
    std::for_each(followers.begin(), followers.end(), [this](Member * other){ other.following.remove(this); });
    std::for_each(following.begin(), following.end(), [this](Member * other){ other.followers.remove(this); });

6 . span style="font-size:14px">CPU throttling in C++?

Answer :

I am not aware of any API to do get the OS's scheduler to do what you want (even if your thread is idle-priority, if there are no higher-priority ready threads, yours will run). However, I think you can improvise a fairly elegant throttling function based on what you are already doing. Essentially (I don't have a Windows dev machine handy):

Pick a default amount of time the thread will sleep each iteration. Then, on each iteration (or on every nth iteration, such that the throttling function doesn't itself become a significant CPU load),

  1. Compute the amount of CPU time your thread used since the last time your throttling function was called (I'll call this dCPU). You can use the GetThreadTimes() API to get the amount of time your thread has been executing.
  2. Compute the amount of real time elapsed since the last time your throttling function was called (I'll call this dClock).
  3. dCPU / dClock is the percent CPU usage (of one CPU). If it is higher than you want, increase your sleep time, if lower, decrease the sleep time.
  4. Have your thread sleep for the computed time.

Depending on how your watchdog computes CPU usage, you might want to use GetProcessAffinityMask() to find out how many CPUs the system has. dCPU / (dClock * CPUs) is the percentage of total CPU time available.

You will still have to pick some magic numbers for the initial sleep time and the increment/decrement amount, but I think this algorithm could be tuned to keep a thread running at fairly close to a determined percent of CPU.

7 . How can i be an expert in c++ ?

Answer :

Years of studying and practice. Do NOT start with Visual Basic, that'll just screw you up. 

If you're completely new to programming, I can recommend the C Primer Plus and C++ Primer Plus by Stephen Prata.

8 . What is the main difference between c and c++?

Answer :

C and C++are two different languages.C++ is more advance then C language.There are many extra feature in C++ like encapsulation,polymorphism and inheritance .

9 . What is c++?explain in more detailed.?

Answer :

C++ (pronounced "See plus plus") is a statically typed, free-form, multi-paradigm, compiled, general-purpose programming language. It is regarded as a middle-level language, as it comprises a combination of both high-level and low-level language features. It was developed by Bjarne Stroustrup starting in 1979 at Bell Labs as an enhancement to the C programming language and originally named "C with Classes". It was renamed C++ in 1983. 

10 . Which is the best compiler c, java, c++?

Answer :

I'm a linux user and use OpenJDK for Java but the best is the one which best meets YOUR needs. The argument for GCC is that it's been the industry standard since the eighties, powering IDEs lilke Dev C++ Eclipse and Code::Blocks in fact. When I say the industry standard though, it's used to compile code for embedded systems, smart phones and Supercomputers. Apple doesn't like among other things the licensing and has released CLang. I just tried it but frankly I'm not impressed yet. Windows has released Visual Studio. Like most of its product line it is tightly integrated into its OS. Now I try not to be an M$ customer. For some people that is a plus and back when I was using it it was decent -- but it has clearly improved. For the Windows Platform GCC has no real advantages over it. 

Turbo C++ and Borland C++ are mostly obsolete and for historical purposes. Seriously, they don't reflect modern 64-bit computers and windowed oses. Beyond that I'd say go with what you like. 

EDIT: I just realized your question could be, is compiled c better compiled java or compiled C++ -- or are the compilers better for C C++ or Java. C and C++ are compiled on the same compilers period. Java is interpreted. You do compile it with javac but the jvm the other answer refers to is the run-time library, not the compiler. Each language is ideally suited for its own -- different -- purpose.

11 . How to put password in a c++ to sing inprogram?

Answer :

Best solution would be to put a key in the registry to indicate the user has activated your software. You can read the key at startup and if not there, prompt. 

Typically that type of information should go into HKCU\Software\<Your App Name>

12 . In C & C++ is there any change only in the following topics?

Answer :

Look c and c++ are very different from each other. C is not an object oriented programming language where as c++ is based on oop.
You won't find any difference in variables operators control flow arrays... But the things such as functions are different as... Due to polymorphism it can be overloaded.. Plus you have the concepts of classes, inheritance, and various other aspects that make it unique for oop. So my recommendation would be to start from basic as me I have done c and,c++ and just started with java.. I don't find it hard to grasp if you have the basic.

13 . Best compiler for c c++?

Answer :

The second link under the Windows section installs both a compiler and and an IDE. I didn't have much luck finding out what the system requirements are for Code Blocks. If the IDE is too slow on your machine, you could try compiling the code using the command line and writing your code using a text editor like this one: 

14 . Why c is used in banks? Why not c++?

Answer :

Because banks are always doing DEDUCTIONS in the name of CHARGES and earning profits....they are not use to give INTEREST. Their goal is to MINUS our balance any how. So how can they use C++ i mean PLUS PLUS. 

So they are using C only. Which is meant for DEDUCTION or MINUS.

15 . span style="font-size:14px">What is c++?

Answer :

c++ is a low/medium level programming language. It is a sucessor to the c programming language beveloped by bell laboratories. 

While c language was s structure oriented language with lots of limitations, the c++ was made to be an object oriented language and is much more powerful versatile yet comparatively easy language to learn. This made it a very favorite programming language for novice level programmers and elite programmers alike.