Why use a perfectly forwarded value (a functor)?

In Brief… The TL;DR, you want to preserve the value category (r-value/l-value nature) of the functor because this can affect the overload resolution, in particular the ref-qualified members. Function definition reduction To focus on the issue of the function being forwarded, I’ve reduced the sample (and made it compile with a C++11 compiler) to; template<class … Read more

SFINAE and partial class template specializations

I would like to argue that the Standard does not support SFINAE in partial specializations, due to a wording defect. Let’s start with [temp.class.spec.match]: A partial specialization matches a given actual template argument list if the template arguments of the partial specialization can be deduced from the actual template argument list (14.8.2). And, from [temp.deduct], … Read more

Why was 1

The relevant issue is CWG 1457, where the justification is that the change allows 1 << 31 to be used in constant expressions: The current wording of 5.8 [expr.shift] paragraph 2 makes it undefined behavior to create the most-negative integer of a given type by left-shifting a (signed) 1 into the sign bit, even though … Read more

Given int **p1 and const int**p2 is p1 == p2 well formed?

Before C++14 this case was ill-formed and the more general case with some exceptions was also ill-formed. This is covered in defect report 1512: Pointer comparison vs qualification conversions , which says: According to 5.9 [expr.rel] paragraph 2, describing pointer comparisons, Pointer conversions (4.10 [conv.ptr]) and qualification conversions (4.4 [conv.qual]) are performed on pointer operands … Read more

Why generic lambdas are allowed while nested structs with templated methods aren’t?

This is core issue 728, which was filed before generic lambdas were a thing. You mentioned generic lambdas and that they were identical to local classes with corresponding member template operator(). However, they actually aren’t, and the differences are related to implementation characteristics. Consider template <typename T> class X { template <typename> void foo() { … Read more

How to move from std::optional

It is valid to move from optional<T>::value() since it returns a mutable reference and the move does not destroy the object. If the optional instance is not engaged, value() will throw a bad_optional_access exception (§20.6.4.5). You explicitly check whether the option is engaged: if (content) Process(move(*content)); But you don’t use the member value() to access … Read more

Detect existence of private member

There is indeed a way for non-final non-union class types: namespace detail { struct P {typedef int member;}; template <typename U> struct test_for_member : U, P { template <typename T=test_for_member, typename = typename T::member> static std::false_type test(int); static std::true_type test(float); }; } template <typename T> using test_for_member = std::integral_constant<bool, decltype(detail::test_for_member<T>::test(0)){}>; Demo. The trick is to … Read more

Does auto return type deduction force multiple functions to have the same return type?

Based on the following, GCC has the right behaviour in this case, but only by coincidence (see below): §7.1.6.4 [dcl.spec.auto]/8 If the init-declarator-list contains more than one init-declarator, they shall all form declarations of variables. Why only by coincidence? The error message is a clue. Changing the functions to deduce the same return type causes … Read more

Are compound literals Standard C++?

This is an extension that both gcc and clang support. The gcc document says: As an extension, GCC supports compound literals in C90 mode and in C++, though the semantics are somewhat different in C++. if you build with -pedantic you should receive a warning, for example clang says (see it live): warning: compound literals … Read more