Favorite little known or underused C++ features

Started by
45 comments, last by SeraphLance 11 years, 5 months ago
Personally I like dropping the comma operator into functions just to upset people who have to read my code.
Advertisement

Personally I like dropping the comma operator into functions just to upset people who have to read my code.

You are Evil Steve ohmy.png
[font=georgia,serif]Duff's device: not exceptionally useful, but clever.[/font]

[size="1"]And a Unix user said rm -rf *.* and all was null and void...|There's no place like 127.0.0.1|The Application "Programmer" has unexpectedly quit. An error of type A.M. has occurred.
[size="2"]

std::get_temporary_buffer & std::return_temporary_buffer.
I am convined nobody has ever used these functions for anything, ever.

That you can read and write from different elements in a union if the elements are structs and have the same prefix structure:


union {
struct {int x, y; };
struct {int u, v; };
}

x = 45;
cout << u;


I also like that the library and language is designed in such a way that the compilers can optimize usage of the library really well; gcc can optimize through almost everything in there.


structures declared within an anonymous union are not legal C++. Its a visual studio extension.

-changed wording slightly

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.


anonymous structures within an anonymous union are not legal C++. Its a visual studio extension.


Crap, I've been looking the standard up and down and I didn't find anything that would make this illegal. Though I could've looked harder. Maybe there's a difference between C11 and C++11 in this regard? But I do know that it's not just a visual studio extension; it works well on gcc and clang as well.
Variadic templates are ridiculously powerful, and antiquate a lot of redundant C++03 template metaprogramming.

Initializer lists are a much needed addition to the language. They add the ability to make classes feel more like POD's, even when they aren't.


std::vector<int> ivec = {1, 2, 3, 4};
//vs.
std::vector<int> ivec;
ivec.push_back(1);
ivec.push_back(2);
ivec.push_back(3);
ivec.push_back(4);

Between Scylla and Charybdis: First Look <-- The game I'm working on

Object-Oriented Programming Sucks <-- The kind of thing I say


[quote name='Washu' timestamp='1351017811' post='4993161']
anonymous structures within an anonymous union are not legal C++. Its a visual studio extension.


Crap, I've been looking the standard up and down and I didn't find anything that would make this illegal. Though I could've looked harder. Maybe there's a difference between C11 and C++11 in this regard? But I do know that it's not just a visual studio extension; it works well on gcc and clang as well.
[/quote]


C++98 - §9.5.2
[Note: nested types and functions cannot be declared within an anonymous union.]

C++11 - §9.5.5
[ Note: Nested types and functions cannot be declared within an anonymous union. —end note ]
[/quote]

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Out of curiosity I'm guessing it was left out of the C++11 standard do to ambiguous naming as functions/variables would live in an unnamed inner scope?

Out of curiosity I'm guessing it was left out of the C++11 standard do to ambiguous naming as functions/variables would live in an unnamed inner scope?


In the anonymous case:


void f() {
union {
struct { int x, y; };
struct { int x, y; };
} u;
}


In the not so anonymous case its harder to come up with the same example, but there are similar issues.

I should note that the only places I've actually seen people use anonymous unions/structures was usually almost always a hack that could have been avoided.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

This topic is closed to new replies.

Advertisement