pmeerw's blog
GNU C++ already provides some features of the upcoming c++0x standard which really make programming more convenient...
// compile with g++ -Wall -std=c++0x newcpp.cpp
// taken from c't 2010/7
// see also http://gcc.gnu.org/projects/cxx0x.html
#include <vector>
#include <iostream>
#include <initializer_list>
#include <tuple>
struct X {
// initializer list
X(std::initializer_list<int> l) : data(new int[l.size()]) {
std::copy(l.begin(), l.end(), data);
}
// disable copy constructor
X(const X &o) = delete;
// make explicit that we use the default assignment operator
X& operator=(const X &o) = default;
int *data;
};
/* not yet
constexpr int f() {
return 42;
}
*/
int main() {
// initializer lists
std::vector<int> v = {1, 2, 3};
X x({1, 2, 3});
// auto type
for (auto i = v.begin(); i != v.end(); i++) {
std::cout << *i << std::endl;
}
/* not yet
for (const auto i : {1, 2, 3}) cout << i << endl;
*/
// enum with scope
enum class Color {Red, Green, Blue};
enum class Alert {Red, Yellow, Green};
// enum with explicit type
enum class Level : short {Low = 3, High = 8};
/* not yet
// null pointer
int *a = nullptr;
*/
// compile-time assert
static_assert(sizeof(int *) == 4, "32bit pointers expected");
// tuple (using variadic template parameters internally)
std::tuple<int, int, int> y{1, 2, 3};
}
posted at: 11:01 | path: /programming | permanent link