infoarena

Comunitate - feedback, proiecte si distractie => Blog => Subiect creat de: Paul-Dan Baltescu din Aprilie 28, 2013, 02:14:28



Titlul: C++ compiler upgrades on infoarena
Scris de: Paul-Dan Baltescu din Aprilie 28, 2013, 02:14:28
http://www.infoarena.ro/blog/cpp11


Titlul: Răspuns: C++ compiler upgrades on infoarena
Scris de: Adrian Budau din Aprilie 28, 2013, 02:19:50
Another cool thing:

Cod:
struct A {
int x = 2, y = 3;
};

No need for constructors anymore for simple stuff.


Titlul: Răspuns: C++ compiler upgrades on infoarena
Scris de: Paunel Cosmin din Aprilie 28, 2013, 02:33:48
Foarte misto, bravo! :D
Pentru al doilea feature se poate si pe set-uri din STL ?


Titlul: Răspuns: C++ compiler upgrades on infoarena
Scris de: Paul-Dan Baltescu din Aprilie 28, 2013, 03:11:31
Yes, you can do range-based for loops on any iterable container. That includes sets and maps:

Cod:
set<int> s = {5, 4, 3, 2, 1};
for (auto x: s) {
  cout << x << endl;
}

map<string, int> m = {{"john", 1}, {"mary", 2}};
for (auto e: m) {
  cout << e.first << " " << e.second << endl;
}


Titlul: Răspuns: C++ compiler upgrades on infoarena
Scris de: Laurentiu Ion din Aprilie 28, 2013, 12:05:53
Good stuff.

It's nice that we also have a standard container for constant sized, C-like, arrays:
Cod:
array<int, 3> arr = {1, 2, 3};
arr.size(); // it has the benefit that it knows its own size and stuff

Now introducing tuple<int,double,string,...>, which are like std::pair, but they can contain any number of elements. Yay, no more annoying custom structs or pairs of pairs of pairs gibberish.

And then there are hash_map and hash_set, which were previously only available in the ext directory, under __gnu_cxx.


Titlul: Răspuns: C++ compiler upgrades on infoarena
Scris de: Adrian Budau din Aprilie 28, 2013, 14:47:17
Another 2 things:

to_string a function which conversts numeric types to std::string and std::tie. WIth std::tie you can do stuff like this

Cod:
auto a = std::make_tuple(0, "Hello world!", 5.5);

int x;
double y;
std::tie(x, std::ignore, y) = a;

And now x and y have the values a.first and a.second.


Titlul: Răspuns: C++ compiler upgrades on infoarena
Scris de: Petenchea Alexandru din Aprilie 28, 2013, 15:05:08
Era si cazul :P . Multumim ! :D

The g++ 4.8 compiler also has some nice optimizer improvements, most of which are available since g++ 4.7 . Inline functions will be optimized even further if the compiler knows some additional information about function's parameters.
Cod:
inline int a (int x)
{
    if (x < 0)
    {
         // big code...
    }
    else
    {
        // big code...
    }
    return x;
}


For a(-1) the compiler will write just half of the code now. This feature is great if you inline large functions which are called many times.

Also non-inline functions are optimized.

Cod:
void f (bool x)
{
  if (!x)
    // code ...
  else
    // code ...
}

int main (void)
{
   // code...
  f(true);
  f(false);
  f(true);
  f(false);
  f(true);
  f(false);
   //code...
}


g++ will now produce two copies of f. One with x being true, while other with x being false. This leads to performance improvements previously possible only by inlining all calls. Cloning causes a lot less code size growth.

A string length optimization pass has been added since g++ 4.7. It attempts to track string lengths and optimize various standard C string functions ( strlen, strchr, strcpy, strcat, stpcpy and so on...).

Cod:
void x (char *a, char *b, char *c, char *d)
{
  strcpy (a, b);
  strcat (a, c);
  strcat (a, d);
}

becomes

void x (char *a, char *b, char *c, char *d)
{
  strcpy (stpcpy (stpcpy (a, b), c), d);
}