Online Book Reader

Home Category

Professional C__ - Marc Gregoire [355]

By Root 1150 0

{

copyFrom(src);

}

template

NDGrid::~NDGrid()

{

delete [] mElems;

mElems = nullptr;

}

template

void NDGrid::copyFrom(const NDGrid& src)

{

mSize = src.mSize;

mElems = new T[mSize];

for (size_t i = 0; i < mSize; i++) {

mElems[i] = src.mElems[i];

}

}

template

NDGrid& NDGrid::operator=(const NDGrid& rhs)

{

// Check for self-assignment.

if (this == &rhs) {

return *this;

}

// Free the old memory.

delete [] mElems;

mElems = nullptr;

// Copy the new memory.

copyFrom(rhs);

return *this;

}

template

void NDGrid::resize(size_t newSize)

{

T* newElems = new T[newSize];

for (size_t i = 0; i < newSize && i < mSize; i++) {

newElems[i] = mElems[i];

// Don't need to resize recursively, because this is the base case.

}

mSize = newSize;

delete [] mElems;

mElems = newElems;

}

template

T& NDGrid::operator[](size_t x)

{

return mElems[x];

}

template

const T& NDGrid::operator[](size_t x) const

{

return mElems[x];

}

Code snippet from NDGrid\NDGrid.h

Now, you can write code like this:

NDGrid my3DGrid;

my3DGrid[2][1][2] = 5;

my3DGrid[1][1][1] = 5;

cout << my3DGrid[2][1][2] << endl;

Code snippet from NDGrid\NDGridTest.cpp

TYPE INFERENCE


Type inference is new in C++11 and allows the compiler to automatically deduce the exact type of an expression. There are two new keywords for type inference: auto and decltype. Type inference turns out to be very useful in combination with templates. This section goes into more detail on their use in a template context.

The auto Keyword

The new auto keyword has two completely different meanings. The first meaning is to tell the compiler to automatically deduce the type of a variable at compile time. The following line shows the simplest use of the auto keyword in that context:

auto x = 123; // x will be of type int

In this example you don’t win much by typing auto instead of int; however, it becomes useful for more complicated types. Suppose you have a map that maps an int to a vector of strings:

std::map> m;

Before C++11, if you wanted a constant iterator to the beginning of this map, you had to type the following:

std::map>::const_iterator citer = m.begin();

With the auto keyword it becomes:

auto citer = m.cbegin();

The second use of the auto keyword is when using the new C++11 alternative function syntax, which is mentioned in Chapter 9. Basically, it allows you to put the return type at the end of the function prototype instead of at the beginning. For example, take the following function:

int func(int i)

{

return i + 2;

}

This can be rewritten by using the alternative function syntax as follows:

auto func(int i) -> int

{

return i + 2;

}

The auto keyword here has a completely different meaning. It states that this function prototype is using the alternative function syntax. If you look at the preceding example you might think that this alternative function syntax doesn’t really add any new value to the language. However, the new syntax becomes important in the context of templates together with the new decltype keyword, as you will see in the next sections.

The decltype Keyword

The decltype keyword takes an expression as argument, and computes the type of that expression. For example:

int x = 123;

decltype(x) y = 456;

In this example, the compiler will make y of type int because that’s the type of x. Just like the auto keyword for the alternative function syntax, the decltype keyword doesn’t seem to add much value on first sight. However, in the context of templates, auto and decltype become pretty powerful.

auto and decltype with Templates

The use of the auto and decltype keywords in combination with templates is best illustrated with an example. The following example defines two classes: MyInt and MyString. These are simple wrappers for an int and a std::string, respectively. Their constructors accept a single value used

Return Main Page Previous Page Next Page

®Online Book Reader