c++0x

Special member functions in C++0x

The Wikipedia article about special member functions doesn't contain any reference to move constructors and move assignment operators. I would like to update the entry but I'm not sure what the 0x standard says. What are the rules regarding these two functions? Are they automatically generated by the compiler and if so when? Edit: I...

push_back of unique_ptr

What's wrong here? #include <memory> #include <vector> int main() { std::vector<std::unique_ptr<int>> vec; int x(1); std::unique_ptr<int> ptr2x(&x); vec.push_back(ptr2x); //This tiny command has a vicious error. return 0; } The error: $ g++ -std=gnu++0x main.cpp In file included from c:\mingw\bin\../lib/gcc/min...

seems as bug in MS VC2010 lambda expressions ?

Behaviour of a lambda expression used in static initializers magically depends on local variables initialized inside lambda body int static_1 = [=]() -> int { int k_=7;// if this statement presents, the lambda doesn't work (static_1 remains uninitialized) return 5; } (); int static_2= [=]() -> int { //Ok wi...

Visual Studio 2010 compile options

I'm trying to compile this source code : // exception_set_unexpected.cpp // compile with: /c /EHsc #include<exception> #include<iostream> using namespace std; void unfunction( ) { cout << "I'll be back." << endl; terminate( ); } int main( ) { unexpected_handler oldHand = set_unexpected( unfunction ); unexpected( ); } ...

Is it possible to inline a lambda expression?

I want to inline a lambda expression since it is very short for performance reason. Is it possible? ...

Problem with weak_ptr comparison in VS10

I can not get 'operator <' to compile for a weak_ptr using VS10. Am I missing an #include or #using? Even the the code sample in the documentation does not work for me. http://msdn.microsoft.com/en-us/library/bb982759.aspx // temp.cpp : Defines the entry point for the console application. // #include "stdafx.h" // std_tr1__memory__o...

On the initialization of std::array

Let's say you have a c++0x std::array member of a template class and you want to initialize it by means of a constructor that takes a couple of iterators: template <typename Tp, size_t N> class Test { public: template <typename Iterator> Test(Iterator first, Iterator last) { if (std::distance(first,last) > N ) ...

std::tuple get() member function

boost::tuple has a get() member function used like this: tuple<int, string, string> t(5, "foo", "bar"); cout << t.get<1>(); // outputs "foo" It seems the C++0x std::tuple does not have this member function, and you have to instead use the non-member function form: std::get<1>(t); which to me looks uglier. Is there any particular ...

C++0x: iterating through a tuple with a function

Hi, I have a function named _push which can handle different parameters, including tuples, and is supposed to return the number of pushed elements. For example, _push(5) should push '5' on the stack (the stack of lua) and return 1 (because one value was pushed), while _push(std::make_tuple(5, "hello")) should push '5' and 'hello' and r...

C++0x lambda, how can I pass as a parameter?

Please look at the following C++0x lambda related code: typedef uint64_t (*WEIGHT_FUNC)(void* param); typedef std::map<std::string, WEIGHT_FUNC> CallbackTable; CallbackTable table; table["rand_weight"] = [](void* param) -> uint64_t { return (rand() % 100 + 1); }; I got an error (in Visual Studio 2010) that the lambda couldn't be co...

Order a container by member with STL

Suppose I have some data stored in a container of unique_ptrs: struct MyData { int id; // a unique id for this particular instance data some_data; // arbitrary additional data }; // ... std::vector<std::unique_ptr<MyData>> my_data_vec; The ordering of my_data_vec is important. Suppose now I have another vector of IDs of My...

array of arrays of different size

Hello, I've some code that produces a set of tr1::array of different sizes, but same type, like array<int, 2> array<int, 4> array<int, 6> The number of these arrays, and their sizes, are given in compile time, so I know exactly how many of them there will be and how's big each one (but they may be different). Problem: I would like to...

C++0x Lambda to function pointer in VS 2010

I am trying to use a lambda to pass in place of a function pointer but VS2010 can't seem to convert it. I have tried using std::function like this and it crashes and I have no idea if I am doing this right! #include <windows.h> #include <conio.h> #include <functional> #include <iostream> #include <concrt.h> void main() { std::f...

How do I debug C++0x programs in MacPorts gcc 4.5?

I have a simple c++ program I am trying to debug, but gdb cannot find the object file for the libraries (or no debug info is available), and it does not seem able to find the debug symbols for my executable either. I am on OSX 10.5.8, with macports, and I compile my code with g++-mp-4.5 -Wall -pedantic -std=c++0x -g -ggdb -I/opt/loc...

Why can't I do tupleVar.get(3) or .get<3>()?

I was going over C++0x. As i looked at tuple I saw this example. Why do I need to do get<3>(var)? Why can't I do var.get(index) or var.get<index>()? I prefer these to make code look and feel consistant. typedef tuple< int, double, long &, const char * > test_tuple ; long lengthy = 12 ; test_tuple proof( 18, 6.5, lengthy, "Ciao!" ) ; len...

Variadic templates for lambda expressions

What's the correct way to do this with g++: template < typename F > void g (F f); template < typename ... A > void h (A ... a); template < typename ... A > void f (A ... a) { g ([&a] () { h (a...); }); // g++-4.6: error: parameter packs not expanded with »...« } ...

Problem with std::make_tuple in C++0x.

Tying to compile the following program with Visual Studio 10, I get lot of compile errors: #include "stdafx.h" #include <tuple> #include <string> #include <map> #include <iostream> int _tmain(int argc, _TCHAR* argv[]) { typedef std::tuple<std::string, std::string> key_t; typedef std::map<key_t, std::string> map_t; map_t t...

Where in the C++ Standard does it say ::delete can change lvalues?

I ran into my first compiler that changes the lvalue passed to ::delete, but doesn't zero out the lvalue. That is the following is true: Foo * p = new Foo(); Foo * q = p; assert(p != 0); assert(p == q); ::delete p; assert(p != q); assert(p != 0); Note that p is not zero after the delete operation, and it has changed from it's o...

Moving ctor and moving dtor.

Hi, as I've asked in here and after a while I've agreed and accepted right answer to that question I was just thinking, if would it be useful to have something like "moving destructor" which would be invoked on moved object everytime we used move ctor or operator=. In this way we would have to specify only in move dtor what we want from...

Initializing std::tuple from initializer list

Hi, I'm wondering whether the tuple can be initialized by initializer list (to be more precise - by initializer_list of initializer_lists)? Considering the tuple definition: typedef std::tuple< std::array<short, 3>, std::array<float, 2>, std::array<unsigned char, 4>, std::arra...