How do we use void_t for SFINAE?

1. Primary Class Template When you write has_member<A>::value, the compiler looks up the name has_member and finds the primary class template, that is, this declaration: template< class , class = void > struct has_member; (In the OP, that’s written as a definition.) The template argument list <A> is compared to the template parameter list of … Read more

Raw pointer lookup for sets of unique_ptrs

In C++14, std::set<Key>::find is a template function if Compare::is_transparent exists. The type you pass in does not need to be Key, just equivalent under your comparator. So write a comparator: template<class T> struct pointer_comp { typedef std::true_type is_transparent; // helper does some magic in order to reduce the number of // pairs of types we … Read more

Compile c++14-code with g++

For gcc 4.8.4 you need to use -std=c++1y in later versions, looks like starting with 5.2 you can use -std=c++14. If we look at the gcc online documents we can find the manuals for each version of gcc and we can see by going to Dialect options for 4.9.3 under the GCC 4.9.3 manual it … Read more

error::make_unique is not a member of ‘std’

make_unique is an upcoming C++14 feature and thus might not be available on your compiler, even if it is C++11 compliant. You can however easily roll your own implementation: template<typename T, typename… Args> std::unique_ptr<T> make_unique(Args&&… args) { return std::unique_ptr<T>(new T(std::forward<Args>(args)…)); } (FYI, here is the final version of make_unique that was voted into C++14. This … Read more

Arity of a generic lambda

This technique will work in some cases. I create a fake_anything type that can fake almost anything, and try to invoke your lambda with some number of instances of that. #include <iostream> struct fake_anything { fake_anything(fake_anything const&); fake_anything(); fake_anything&operator=(fake_anything const&); template<class T>operator T&() const; template<class T>operator T&&() const; template<class T>operator T const&() const; template<class T>operator … Read more

Optimization of raw new[]/delete[] vs std::vector

When using a pointer to a dynamically allocated array (directly using new[] and delete[]), the compiler optimized away the calls to operator new and operator delete even though they have observable side effects. This optimization is allowed by the C++ standard section 5.3.4 paragraph 10: An implementation is allowed to omit a call to a … Read more

Compile-time or runtime detection within a constexpr function

It is possible to detect if a given function-call expression is a constant expression, and thereby select between two different implementations. Requires C++14 for the generic lambda used below. (This answer grew out this answer from @Yakk to a question I asked last year). I’m not sure how far I’m pushing the Standard. This is … 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

Is it defined behavior to reference an early member from a later member expression during aggregate initialization?

Your second case is undefined behavior, you are no longer using aggregate initialization, it is still list initialization but in this case you have a user defined constructor which is being called. In order to pass the second argument to your constructor it needs to evaluate foo.i but it is not initialized yet since you … Read more

Throw in constexpr function

clang is correct, note the HEAD revision of gcc accepts also accepts this code. This is a well-formed constexpr function, as long as there is value for the argument(s) that allows the function to be evaluated as a core constant expression. In your case 1 is such a value. This is covered in the draft … Read more