c++0x

What do you dislike most about some of your favorite C++0x features?

While I am excited that C++0x will be much more fun to program in than C++98, I sometimes feel that some of the new features leave something important to be desired. For e.g. initializer lists can not be deduced as references to arrays, variadic templates don't allow for member packs or arbitrary expression expansion, local classes can't...

how to write recursive lambda functions in c++0x

i am new to c++0x, so please excuse me if my question is silly :). i am writing the following recursive lambda function, it doesn't compile. sum.cpp #include <iostream> #include <functional> auto term = [](int a)->int { return a*a; }; auto next = [](int a)->int { return ++a; }; auto sum = [term,next,&sum](int a, int b)mutable -...

How to get an object of a unknown class with given classname

I am searching for a way to determine at runtime, which type of object should be alloced (based on a given class name, which is of type const char*). Well the simplest way of course is to use loads of ifs /else ifs, but that doesnt seem applicable, because i have > 100 different classes(well at least they all derive from one base class)...

Is there an algorithm for moving ranges?

In C++98, I can copy ranges with the std::copy algorithm. std::copy(source.begin(), source.end(), destination.begin()); Is there an algorithm in C++0x that moves the elements from source to destination? Or is std::copy somehow overloaded to accept something like rvalue iterators -- is there even such a thing? The algorithm might look...

Why aren't there compiler-generated swap() methods in C++0x?

C++ compilers automatically generate copy constructors and copy-assignment operators. Why not swap too? These days the preferred method for implementing the copy-assignment operator is the copy-and-swap idiom: T& operator=(const T& other) { T copy(other); swap(copy); return *this; } (ignoring the copy-elision-friendly for...

Compile time string hashing

I have read in few different places that using c++0x's new string literals it might be possible to compute a string's hash at compile time. However, no one seems to be ready to come out and say that it will be possible or how it would be done. Is this possible? What would the operator look like? I'm particularly interested use cas...

Check if C++0x parameter pack contains a type

I was wondering if C++0x provides any built-in capabilities to check if a parameter pack of a variadic template contains a specific type. Today, boost:::mpl::contains can be used to accomplish this if you are using boost::mpl::vector as a substitute for variadic templates proper. However, it has serious compilation-time overhead. I suppo...

Any RAII template in boost or C++0x

Is there any template available in boost for RAII. There are classes like scoped_ptr, shared_ptr which basically work on pointer. Can those classes be used for any other resources other than pointers. Is there any template which works with a general resources. Take for example some resource which is acquired in the beginning of a scope ...

Are function-local typedefs visible inside C++0x lambdas?

Sorry to bump with a side-question, but although the bug reports indicate them as fixed I still cannot get any of the code to work, crash and all. Have I installed the final release incorrectly or are they still not publicly fixed? :( I've run into a strange problem. The following simplified code reproduces the problem in MSVC 2010 Be...

C++0x move constructor gotcha

The C++ move constructor and move assignment operator seem like really positive things. And they can be used in situations where the copy constructor makes no sense because they don't require duplicating resources being pointed at. But there are cases where they will bite you if you aren't careful. And this is especially relevant as I...

Lambdas don't appear to work within ref classes in VS2010

One of the cool new C++ features in Visual Studio 2010 are lambda expressions. However, I can't get them to work within a managed class. class UnmanagedClass { void Foo() { // Creating empty lambda within unmanaged class. // This compiles fine. auto lambda = [](){ ; }; } }; ref class ManagedClass { v...

C++0x "move from" container

In C++0x, we get an efficiency boost concerning containers with std::move: SomeExpensiveType x = /* ... */; vec.push_back(std::move(x)); But I can't find anything going the other way. What I mean is something like this: SomeExpensiveType x = vec.back(); // copy! vec.pop_back(); // argh This is more frequent (the copy-pop) on adapte...

non-integral constants

I want a header file with a non-integral constant in it, e.g. a class. Note the constant does not need to be a compile-time constant. static const std::string Ten = "10"; This compiles but is undesirable as each compilation unit now has its own copy of Ten. const std::string Ten = "10"; This will compile but will fail with a linke...

non-class rvalues always have cv-unqualified types

§3.10 section 9 says "non-class rvalues always have cv-unqualified types". That made me wonder... int foo() { return 5; } const int bar() { return 5; } void pass_int(int&& i) { std::cout << "rvalue\n"; } void pass_int(const int&& i) { std::cout << "const rvalue\n"; } int main() { pass_int(foo()); // prints "rvalu...

Why does this C++0x program generates unexpected output?

This program: test_header.hpp #include <boost/signal.hpp> #include <utility> class Sensor; class Recorder : public ::boost::signals::trackable { public: explicit Recorder(int id) : id_(id) {} // Cannot be copied Recorder(const Recorder &) = delete; Recorder &operator =(const Recorder &) = delete; // But can be moved...

How to slice with for-range loop ? C++0x

Using range based for loops in C++0X, I know we'll be able to do : std::vector<int> numbers = generateNumbers(); for( int k : numbers ) { processNumber( k ); } (might be even simpler to write with lambda) But how should i do if I only want to apply processNumber( k ) to a part of numbers? For example, how should I write this for ...

Error avalanche in Boost.Spirit.Qi usage

Hi, I'm not being able to figure out what's wrong with my code. Boost's templates are making me go crazy! I can't make heads or tails out of all this, so I just had to ask. What's wrong with this? #include <iostream> #include <boost/lambda/lambda.hpp> #include <boost/spirit/include/qi.hpp> void parsePathTest(const std::string &path) ...

Will `typedef enum {} t` allow scoped enum element identifiers in C++0x?

I believe the new C++ standard allows for an extra "scope" for enumerated types: enum E { e1, e2 }; E var = E::e1; Since I know lots of source files containing the old C-style enum typedef, I wondered if the new standard would allow using the typedef for these otherwise anonymous enumerated types: typedef enum { d1, d2 } D; D var = ...

How do I pass a C++0x random number generator to a function?

Do they all inherit from a base class? Do I have to use templates? (I am referring to these http://www.codeguru.com/cpp/cpp/cpp_mfc/stl/article.php/c15319/) I am doing this right now: typedef std::mt19937 RNG; and then class Chooser { public: Chooser(RNG& rng, uint n, uint min_choices, uint max_choices): In other words, I'm ...

boost::lock_guard vs boost::mutex::scoped_lock

Which is preferred boost::lock_guard or boost::mutex::scoped_lock? I'm using Boost.Thread with the hope to move to C++0x threading when it becomes available. Is scoped_lock part of the next c++ standard? Are the any advantages to prefer one over the other? NOTE: I'm aware that scoped_lock is just a typedef of lock_guard. edit:...