Favorite little known or underused C++ features
Personally I like dropping the comma operator into functions just to upset people who have to read my code.
Personally I like dropping the comma operator into functions just to upset people who have to read my code.
You are Evil Steve
std::get_temporary_buffer & std::return_temporary_buffer.
I am convined nobody has ever used these functions for anything, ever.
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
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.
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);
[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]
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement