Why is std::make_unique not implemented using list initialization?

In C++20, this will compile: std::make_unique<point>(1, 2); due to the new rule allowing initializing aggregates from a parenthesized list of values. In C++17, you can just do: std::unique_ptr<point>(new point{1, 2}); That won’t work with make_shared though. So you can also just create a factory (forwarding left as an exercise): template <typename… Args> struct braced_init { … Read more

STL way to access more elements at the same time in a loop over a container

The most STLish way I can imagine: std::partial_sum(std::begin(v), std::end(v), std::begin(v), std::multiplies<double>()); Example: #include <iostream> #include <vector> #include <iterator> #include <numeric> #include <functional> int main() { std::vector<double> v{ 1.0, 2.0, 3.0, 4.0 }; std::partial_sum(std::begin(v), std::end(v), std::begin(v), std::multiplies<double>()); std::copy(std::begin(v), std::end(v), std::ostream_iterator<double>(std::cout, ” “)); } Output: 1 2 6 24 Live demo link.

Placement new in std::aligned_storage?

The most paranoid way is ::new ((void *)::std::addressof(storage)) T(a, b, c); Explanation: ::std::addressof guards against overloaded unary operator& on storage, which is technically allowed by the standard. (Though no sane implementation would do it.) The ::std guards against any non-top-level namespaces (or classes) called std that might be in scope. (void *) (which in this … Read more

What are the 6 dots in template parameter packs? [duplicate]

Why does libstdc++ use … … in it’s implementation of is_function? If we check out the cppreference section for std::is_function it gives a sample implementation and says for the first … … case: // specialization for variadic functions such as std::printf template<class Ret, class… Args> struct is_function<Ret(Args……)> : std::true_type {}; so we need the second … Read more

Which parts of the C++14 Standard Library could be and which parts will be made constexpr?

which parts of the Standard Library will now be marked constexpr? From the draft that I’ve looked at for C++14, N3690, the following will be changed to constexpr thus far (In comparison with the C++11 standard)†: std::error_category‘s default constructor std::forward std::move std::move_if_noexcept All of std::pair‘s operator comparisons std::get for std::pair and std::tuple. std::make_tuple All of … Read more

Return value of placement new

I’m surprised no one has mentioned this cheap counterexample yet: struct foo { static foo f; // might seem dubious but doesn’t violate anything void* operator new(size_t, void* p) {return &f;} }; Demo on Coliru. Unless a class-specific placement version is called, however, your assertion should hold. Both expressions have to represent the same address … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)