c++0x

Is it bad form to provide only a move constructor?

I would like to return a noncopyable object of type Foo from a function. This is basically a helper object which the caller will use to perform a set of actions, with a destructor to perform some cleanup after the actions are complete. Before the advent of rvalue references, I would have returned a shared_ptr<Foo> or something similar. ...

Is there some ninja trick to make a variable constant after its declaration?

I know the answer is 99.99% no, but I figured it was worth a try, you never know. void SomeFunction(int a) { // Here some processing happens on a, for example: a *= 50; a %= 10; if(example()) a = 0; // From this point on I want to make "a" const; I don't want to allow // any code past this comment to modif...

C++0x scope exit guard, a good idea?

I've written a small utility class for C++0x which i use as a scope guard for easier handling exception safety and similar. Seems somewhat like a hack. But I'm suprised i haven't seen it somewhere else using C++0x features. I think boost has something similar for C++98. But is it a good idea? Or are there potential problems I have miss...

Passing variadic class template's sub-classes to function that only accepts the base class (via parameter pack deduction/inference)

**I've gotten a few suggestions to make my function pure generic, which would work, but I'd prefer limiting the function to only accept Base and its children. Having trouble making a function that can accept arguments of a variadic template class base type, while the function will actually be called with classes that derive from Base. ...

C++0x and Friend functions and boost::make_shared

If I have a class with a private construction, using boost::make_shared to construct a shared_ptr of that class from within a member function of that class will issue a compiler error using gcc 4.6. #include "boost/shared_ptr.hpp" #include "boost/make_shared.hpp" class Foo { private: Foo(int a){}; public: static boo...

Is std::array<T, S> guaranteed to be POD if T is POD?

I'm currently writing a C++ memory editing library and for the read/write APIs I use type traits (std::is_pod, std::is_same) and boost::enable_if to provide 3 overloads: POD types. e.g. MyMem.Read(SomeAddress); String types. e.g. MyMem.Read>(SomeAddress); (This doesn't actually read out a C++ string, it reads out a C-style string and c...

Converting a lambda to a std::tr1::function

Using visual studio 2008 with the tr1 service pack and Intel C++ Compiler 11.1.071 [IA-32], this is related to my other question I'm attempting to write a functional map for c++ which would work somewhat like the ruby version strings = [2,4].map { |e| e.to_s } So i've defined the following function in the VlcFunctional namespace te...

Implementing for(auto item : container) in VC2010

I wanted to create a little macro to simulate for(auto item : container) in VC2010 which I can then replace with the real construct when it's released. There is BOOST_FOREACH, however I would like auto support. I've tried creating a macro. However it fails when the dereferenced iterator is a constant type. #define _LIB_FOREACH_LINENAM...

Is there any way to use a variable in a lambda which isn't in the current scope?

Basically, I want to know if something like the below is possible? If this isn't possible is there any way to fake it? #include <iostream> using namespace std; template<typename Functor> void foo(Functor func) { auto test = [](Functor f){ int i = 5; f(); }; test(func); } int main() { foo([](){ cout << i << endl;}); } ...

Passing a lambda to a template function on VC10

I wrote the following code on VC10. Calling f1 is okay, but on calling f2 the compiler showed an error. The difference between the two functions is only "template ", but the template type is actually not used. Why does the error occur? #include <functional> void f1( std::tr1::function<void()> f) { } template <typename > void f2( std::...

Can I Have Polymorphic Containers With Value Semantics in C++0x?

This is a sequel to a related post which asked the eternal question: Can I have polymorphic containers with value semantics in C++? The question was asked slightly incorrectly. It should have been more like: Can I have STL containers of a base type stored by-value in which the elements exhibit polymorphic behavior? If you ar...

Is "for each" Microsoft specific?

Visual C++ 2010 accepts: std::vector<int> v; v.push_back(1); v.push_back(2); v.push_back(3); for each (auto i in v) std::cout << i << std::endl; Is this a C++0x feature or a Microsoft extension? According to Wikipedia, the syntax of C++0x's for-each loop different: int myint[] = {1,2,3,4,5}; for (int& i: myint) { std::cout <<...

May std::tuple_element double as a universal template argument retriever?

This question got me thinking. Sometimes it's useful to grab an actual argument from a class template specialization, if it fails to define a public typedef of the argument. In C++03 it's a sign of either bad template design, or contrary design intent, and not particularly common. But variadic templates make typedef coverage impossible, ...

What is "= delete"?

What do these two strange lines of code mean? thread_guard(thread_guard const&) = delete; thread_guard& operator=(thread_guard const&) = delete; ...

Specifying one type for all arguments passed to variadic function or variadic template function w/out using array, vector, structs, etc?

I'm creating a function (possibly member function, not that it matters... maybe it does?) that needs to accept an unknown number of arguments, but I want all of them to be the same type. I know I could pass in an array or vector, but I want to be able to accept the list of args directly without extra structure or even extra brackets. I...

How to generate random numbers in C++ using <random> header members?

I learned to program in C# and have started to learn C++. I'm using the Visual Studio 2010 IDE. I am trying to generate random numbers with the distribution classes available in <random>. For example I tried doing the following: #include <random> std::normal_distribution<double> *normal = new normal_distribution<double>(0.0, 0.0); s...

Do rvalue references allow dangling references?

Consider the below. #include <string> using std::string; string middle_name () { return "Jaan"; } int main() { string&& danger = middle_name(); // ?! return 0; } This doesn't compute anything, but it compiles without error and demonstrates something that I find confusing: danger is a dangling reference, isn't it? ...

capture member variable by value

How would I catch a member variable by value when using C++0x lambdas? Using the [=my_member] syntax doesn't seem to work, and implicit capture uses the "this" pointer. What is need is a way to explicitly specify capture type of member variables. Is that possible? My workaround for now is: void member_function() { std::shared_ptr<...

char and initializer lists

I'd like to pass some numeric byte values via an initializer list a variadic template into an array. Is that possible? template < int N > struct a { char s[N]; template < typename ... A > a (A ... _a) : s {_a...} {} }; int main () { // g++-4.5: error: narrowing conversion of »_a#0« from »int« to »char« inside { } a < 3 > x { ...

Working around the C++ limitation on non-const references to temporaries

I've got a C++ data-structure that is a required "scratchpad" for other computations. It's not long-lived, and it's not frequently used so not performance critical. However, it includes a random number generator amongst other updatable tracking fields, and while the actual value of the generator isn't important, it is important that th...