Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Incomprehensible syntax, from vc++ to c#

I have the following code, which is vital to understand a class I'm working on translating from visual c++ to c#

typedef Rect<double, 2> Rect2;

I simply can't understand what does it mean. I have come to understand that vc++ uses 'stl' or 'std' and a 'vector' class, which is similar to c# list or arraylist, but the syntax up completely eludes me.

By the way, Rect definition is written like this

template<class Real, int Dim>
class Rect {

Rect is a class in one of the vc++ project files, but I can't understand what's the point of this typedef. It's 200+ lines so here's the declaration start

template<class Real, int Dim>
class Rect {
public:

typedef Vector<Real, Dim> Vec;
typedef Rect<Real, Dim> Self;
typedef _RectPrivate::RectOp<Dim> RO;

Rect() : empty(true) {}
Rect(const Vec &vec) : empty(false), lo(vec), hi(vec) {}
Rect(const Vec &inLo, const Vec &inHi) : lo(inLo), hi(inHi) { markEmpty(); }
Rect(const Rect &inRect) : empty(inRect.empty), lo(inRect.lo), hi(inRect.hi) {}
template<class R> Rect(const Rect<R, Dim> &inRect) : empty(inRect.empty), lo(inRect.lo), hi(inRect.hi) {}

Any help appreciated.

like image 822
roamcel Avatar asked Dec 22 '22 07:12

roamcel


1 Answers

Based on your comments to the other answer, the problem you're facing is that you do not understand what C++ templates are. They are actually quite different from C# generics. They are conceptually similar, and syntactically similar, but their actual implementation is very different.

The easiest way to think about a template in C++ is as a "search and replace" mechanism. When you see

typedef Rect<double, 2> Rect2;

That means go find the template class for Rect that takes two template arguments. Here it is:

template<class Real, int Dim>
class Rect {

OK, so "double" corresponds to "class Real" and "2" corresponds to "int Dim". Sure enough, double is a type and 2 is an int, so that works properly. Now go through the template class and replace all instances of "Real" with "double" and "Dim" with 2. The result is:

typedef /* now we start replacing double for Real and 2 for Dim */ 
class Rect {
public:
typedef Vector<double, double> Vec;
typedef Rect<double, 2> Self;
typedef _RectPrivate::RectOp<2> RO;
Rect() : empty(true) {}
Rect(const Vec &vec) : empty(false), lo(vec), hi(vec) {}
...
} Rect2;

Notice that the result of search-and-replace on the template itself both defines and uses more templates, which have to themselves be search-and-replaced to construct them. We'd do a search-and-replace on the Vector template, and so on.

The "typedef" just means "when I say Rect2 I mean Rect<double, 2>". The same way in C# you can say

using MyStringList = System.Collections.Generic.List<string>;

There are many differences between C# generics and C++ templates. The obvious difference here is that C# generics can only be parameterized with types, never with values as is done in this template. Another difference is that C++ templates are fully constructed at compile time, and need only be legally constructable with the actual arguments given in the program. Whereas C# generics must be constructable with any arguments that meet the stated constraints, and the new code generation does not happen until runtime.

like image 110
Eric Lippert Avatar answered Jan 04 '23 17:01

Eric Lippert