What is the purpose of std::scoped_allocator_adaptor?

If you want a container of strings and want to use the same allocator for the container and its elements (so they are all allocated in the same arena, as TemplateRex describes) then you can do that manually: template<typename T> using Allocator = SomeFancyAllocator<T>; using String = std::basic_string<char, std::char_traits<char>, Allocator<char>>; using Vector = std::vector<String, Allocator<String>>; … Read more

What are the uses of the type `std::nullptr_t`?

If more than one overload accepts a pointer type, an overload for std::nullptr_t is necessary to accept a nullptr argument. Without the std::nullptr_t overload, it would be ambiguous which pointer overload should be selected when passed nullptr. Example: void f(int *intp) { // Passed an int pointer } void f(char *charp) { // Passed a … Read more

How to replicate map, filter and reduce behaviors in C++ using STL?

You can use std::transform to do mapping, and std::copy_if for filtering. You have two options for reduce depending on your input and if you want to use a specific type of execution model. I’ve written some simple examples below to demonstrate common use cases. Note that there are multiple overloads for all these algorithms that … Read more

Forward declaration of lambdas in C++

You can’t separate declaration and definition of lambdas, neither forward declare it. Its type is a unique unnamed closure type which is declared with the lambda expression. But you could do that with std::function objects, which is designed to be able to store any callable target, including lambdas. As your sample code shown you’ve been … Read more

How to use std::atomic

You need to make the x attribute atomic, and not your whole class, as followed: class A { std::atomic<int> x; public: A() { x=0; } void Add() { x++; } void Sub() { x–; } }; The error you get in you original code is completely normal: there is no std::atomic<A>::Add method (see here) unless … Read more

Is is_constexpr possible in C++11?

I once wrote it (EDIT: see below for limitations and explanations). From https://stackoverflow.com/a/10287598/34509 : template<typename T> constexpr typename remove_reference<T>::type makeprval(T && t) { return t; } #define isprvalconstexpr(e) noexcept(makeprval(e)) However there are many kinds of constant expressions. The above answer detects prvalue constant expressions. Explanation The noexcept(e) expression gives false iff e contains a potentially … Read more

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