Overload resolution with std::function

In C++11… Let’s take a look at the specification of the constructor template of std::function (which takes any Callable): [func.wrap.func.con]/7-10 template<class F> function(F f); template <class F, class A> function(allocator_arg_t, const A& a, F f); 7 Requires: F shall be CopyConstructible. f shall be Callable (20.10.11.2) for argument types ArgTypes and return type R. The … Read more

should std::common_type use std::decay?

should std::common_type use std::decay? Yes, see Library Working Group Defect #2141. Short version (long version, see link above): declval<A>() returns a A&& common_type is specified via declval, n3337: template <class T, class U> struct common_type<T, U> { typedef decltype(true ? declval<T>() : declval<U>()) type; }; common_type<int, int>::type therefore yields int&&, which is unexpected proposed resolution … Read more

Using Boost adaptors with C++11 lambdas

http://smellegantcode.wordpress.com/2011/10/31/linq-to-c-or-something-much-better/ But you can use this, that works well. #include <boost/range/adaptors.hpp> #include <boost/range/algorithm.hpp> #include <vector> #include <functional> int main() { std::vector<int> v{ 1,5,4,2,8,5,3,7,9 }; std::function<int(int)> func = [](int i) { return -i; }; std::cout << *boost::min_element(v | boost::adaptors::transformed( func)) << std::endl; return 0; } http://liveworkspace.org/code/b78b3f7d05049515ac207e0c12054c70 #define BOOST_RESULT_OF_USE_DECLTYPE works fine in VS2012 for example.

get part of std::tuple

With help of a compile-time integer list: #include <cstdlib> template <size_t… n> struct ct_integers_list { template <size_t m> struct push_back { typedef ct_integers_list<n…, m> type; }; }; template <size_t max> struct ct_iota_1 { typedef typename ct_iota_1<max-1>::type::template push_back<max>::type type; }; template <> struct ct_iota_1<0> { typedef ct_integers_list<> type; }; We could construct the tail simply by … Read more

get part of std::tuple

With help of a compile-time integer list: #include <cstdlib> template <size_t… n> struct ct_integers_list { template <size_t m> struct push_back { typedef ct_integers_list<n…, m> type; }; }; template <size_t max> struct ct_iota_1 { typedef typename ct_iota_1<max-1>::type::template push_back<max>::type type; }; template <> struct ct_iota_1<0> { typedef ct_integers_list<> type; }; We could construct the tail simply by … Read more