Are there any macros to determine if my code is being compiled to Windows? [duplicate]

[Edit: I assume you want to use compile-time macros to determine which environment you’re on. Maybe you want to determine if you’re running on Wine under Linux or something instead of Windows, but in general, your compiler targets a specific environment, and that is either Windows (DOS) or it isn’t, but it’s rarely (never?) both.] … Read more

Solving random crashes

Try Valgrind (it’s free, open-source): The Valgrind distribution currently includes six production-quality tools: a memory error detector, two thread error detectors, a cache and branch-prediction profiler, a call-graph generating cache profiler, and a heap profiler. It also includes two experimental tools: a heap/stack/global array overrun detector, and a SimPoint basic block vector generator. It runs … Read more

“Roll-Back” or Undo Any Manipulators Applied To A Stream Without Knowing What The Manipulators Were [duplicate]

Yes. You can save the state and restore it: #include <iostream> #include <iomanip> using namespace std; int main() { std::ios state(NULL); state.copyfmt(std::cout); cout << “Hello” << hex << 42 << “\n”; // now i want to “roll-back” cout to whatever state it was in // before the code above, *without* having to know what modifiers … Read more

Why doesn’t this “undefined extern variable” result in a linker error in C++17?

Because the variable isn’t odr-used. You have a constexpr if there that always discards the branch that could use it. One of the points of constexpr if is that the discarded branch need not even compile, only be well-formed. That’s how we can place calls to non-existing member functions in a discarded branch.

Am I using the copy_if wrong?

The copy_if algorithm looks something like this(taken from MSVC2010): template<class InIt, class OutIt, class Pr> inline OutIt copy_if(InIt First, InIt Last, OutIt Dest, Pr Pred) { for (; First != _Last; ++First) if (Pred(*_First)) *Dest++ = *First; return (Dest); } And as you can see the copy_if does not do a push_back, it just copy … Read more

c++, usleep() is obsolete, workarounds for Windows/MingW?

I used this code from (originally from here): #include <windows.h> void usleep(__int64 usec) { HANDLE timer; LARGE_INTEGER ft; ft.QuadPart = -(10*usec); // Convert to 100 nanosecond interval, negative value indicates relative time timer = CreateWaitableTimer(NULL, TRUE, NULL); SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0); WaitForSingleObject(timer, INFINITE); CloseHandle(timer); } Note that SetWaitableTimer() uses “100 nanosecond intervals … … Read more

How do I find an element position in std::vector?

Take a look at the answers provided for this question: Invalid value for size_t?. Also you can use std::find_if with std::distance to get the index. std::vector<type>::iterator iter = std::find_if(vec.begin(), vec.end(), comparisonFunc); size_t index = std::distance(vec.begin(), iter); if(index == vec.size()) { //invalid }

C++: max integer [duplicate]

In the C++ standard library header <limits>, you will find: std::numeric_limits<int>::max() Which will tell you the maximum value that can be stored in a variable of type int. numeric_limits is a class template, and you can pass it any of the numeric types to get the maximum value that they can hold. The numeric_limits class … Read more

C++ DLL Export: Decorated/Mangled names

Instead of using .def file just insert pragma comment like this #pragma comment(linker, “/EXPORT:SomeFunction=_SomeFunction@@@23mangledstuff#@@@@”) Edit: Or even easier: Inside the body of the function use #pragma comment(linker, “/EXPORT:” __FUNCTION__”=” __FUNCDNAME__) . . . if you have troubles finding the decorated function name. This last pragma can be further reduced with a simple macro definition.