Making Enumerable Classes

Tags

,

The current realisation of C++ includes a new kind of loop, named “Range-based for Statement” [1, 2].

It has a form of “for( declaration : expression ) statement” and allows simplification of code. For example, if we have a vector and iterate its elements using a classical for:

vector<int> v = { 10, 11, 12, 13, 14 };
// print all of elements
for( auto iter = v.begin(); iter != v.end(); ++iter )
{
	int element = *iter;
	cout << element << endl;
}

then we can use the new variant of for:

vector<int> v = { 10, 11, 12, 13, 14 };
// print all of elements
for( int element : v )
{
	cout << element << endl;
}

Looks like an influence of Continue reading

Does “Templatisation” Always Result in Equivalent Code?

Tags

,

It is natural to believe that when we convert a code to template, it must operate similarly. For example, some “int Max(int a, int b)” function, originally written for int, will work well with long, double etc. after conversion to template.

However, a simple replacement of types with template parameters does not always result in code that works analogously.
Continue reading

Obtaining Function Pointers from Lambdas in C++

Tags

, , ,

Probably you will rarely meet the problem of converting any lambda or std::function to a native function pointer. This could be necessary, for example, when you have to pass a “callback function” to some Windows function, such as EnumWindows or SetTimer.

In managed world of .NET (in C#, VB.NET), the Marshal.GetFunctionPointerForDelegate function is the one that allows the conversion of any lambda (via delegates) to a function pointer. In this investigation we will consider a similar conversion for native C++ lambdas.

Continue reading

Compiling 64-bit Assembler Code in Visual Studio 2013

Tags

, ,

Sometimes or sometime you might like the idea to write some portions of program in Assembler, so using the low-level microprocessor instructions directly, instead of higher-level intermediates such as C++. Usually this is done for achieving a better performance. However, writing code in Assembler does not automatically increases the speed. The results depend on used algorithms and the quality of implementation.

Continue reading

Getting the right output of child console applications in C#

Tags

, ,

Purpose

You develop a Form application, which will start some console application (maybe made by you too), or an Operating System (OS) command (e.g. “dir”), or a command file (.cmd or .bat), and you want to intercept its output.

The result could contain various alphabets and you do not want to lose or distort any letter.

Continue reading

Automatic Disposal of Resources in C++/CLI and C#

Tags

,

If a complex object acquires a relatively large amount of system resources (memory, files, fonts, etc.), then we have to ensure that the resources are freed when the object is not needed anymore, allowing the released resources to be borrowed by other objects.

If we leave the System (“Garbage Collector”) to free the resources for us, then it could do it too late, because such phases occur unpredictable.

Continue reading

Are C# delegates similar to C++ function pointers?

Tags

, , ,

It is commonly known that a C# delegate is very similar to a C++ function pointer.

Compare the next examples in both of the languages:

In C# In C++
// declare the delegate type:
delegate void MyDelegate();

// some compatible function:
void SomeFunction()
{
    Console.WriteLine("Hello, world!");
}

. . . . .

// declare and initialise a delegate variable:
MyDelegate myVariable = SomeFunction;

// call the pointed function:
myVariable();
//
// declare the function pointer type:
typedef void (*MyFunctionPointer)();

// some compatible function:
void SomeFunction()
{
    cout << "Hello, world!" << endl;
}

. . . . .

// declare and initialise a function pointer:
MyFunctionPointer myVariable = &SomeFunction;

// call the pointed function:
myVariable();
//

So, they appear analogous.

However there is a noteworthy difference. Continue reading