C++ 11: playing around with templates, namespaces and enums

Have you lastly wondered how amazing is C++ 11? If you haven’t, then it might be a good time for doing so.

C++ has always been the giant among the most used languages worldwide. But  C++11 rocks! It’s not only because it’s one of the most recently revisions, but a newer language in its own. And I’m speaking from the point of view of an embedded and electronics engineer. An urban legend says that C++ generated code is bigger than that generated from C, so it’s not suitable for embedded systems. That’s a lie, a big one!

Besides of being a C++ fan, I’m also fan of real time kernels. FreeRTOS is one of my favorites. Have you seen its source code? It sucks! I mean, the overall functionality is great, but the source code sucks. I’ve mentioned this kernel because it comes to my mind the idea of how it looks like if it were written in C++ 11. And one of my midterm goals is to write a fully static kernel in C++ 11.

That’s way I started studying C++ 11 deeper than usual. And everything I learn about it I’ll write it here down as often as posible. In this first entry I’ll show a small code that combines templates, namespaces and enums, so I can incorporate them in my kernel code later on.

namespace myNS
{
enum class Colors {BLACK, WHITE};
}

namespace myNS
{

template<class T>
class C
{
public:

void foo(T val);
void goo(Colors val);
};

template<class T>
void C<T>::foo(T val)
{

}

template<class T>
void C<T>::goo(Colors val)
{

}

} // end of myNS


int main(int argc, const char *argv[])
{
  myNS::C<int> o;

  o.foo(1);
  o.goo(myNS::Colors::BLACK);

  return 0;
}

This code compiles with the following statement (as long as you copy and save it with the file name main.cpp, and you have GNU g++ installed):

$ g++ -std=c++11 -Wall -o main.out main.cpp

Anuncios

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s