std::string s1 {“Modern C++”, 3} vs std::string s1 {str, 3}

From: https://en.cppreference.com/w/cpp/string/basic_string/basic_string std::string s1 {“Modern C++”, 3}; Uses the following constructor: basic_string( const CharT* s, size_type count, const Allocator& alloc = Allocator() ); So takes 3 chars to get Mod. std::string s2 {str, 3}; will use the following constructor: basic_string( const basic_string& other, size_type pos, const Allocator& alloc = Allocator() ); So taking the string … Read more

Where to use std::variant over union?

Generally speaking, you should prefer variant unless one of the following comes up: You’re cheating. You’re doing type-punning or other things that are UB but you’re hoping your compiler won’t break your code. You’re doing some of the pseudo-punnery that C++ unions are allowed to do: conversion between layout-compatible types or between common initial sequences. … Read more

How can I emulate destructuring in C++?

In C++17 this is called structured bindings, which allows for the following: struct animal { std::string species; int weight; std::string sound; }; int main() { auto pluto = animal { “dog”, 23, “woof” }; auto [ species, weight, sound ] = pluto; std::cout << “species=” << species << ” weight=” << weight << ” sound=” … Read more

Why is the construction of std::optional more expensive than a std::pair?

libstdc++ apparently does not implement P0602 “variant and optional should propagate copy/move triviality”. You can verify this with: static_assert(std::is_trivially_copyable_v<std::optional<int>>); which fails for libstdc++, and passes for libc++ and the MSVC standard library (which really needs a proper name so we don’t have to call it either “The MSVC implementation of the C++ standard library” or … Read more

Why has std::reduce been added in C++17?

Since you asked for a thorough explanation, and the previous answer covers only basics, I’m taking the liberty of adding a more thorough one. std::reduce is intended to perform the second major step of the MapReduce programming model. The basic idea is that the platform (in this case, C++ implementation) provides these two primitive operations … Read more

How the new range-based for loop in C++17 helps Ranges TS?

C++11/14 range-for was overconstrained… The WG21 paper for this is P0184R0 which has the following motivation: The existing range-based for loop is over-constrained. The end iterator is never incremented, decremented, or dereferenced. Requiring it to be an iterator serves no practical purpose. As you can see from the Standardese that you posted, the end iterator … Read more

Cv-qualifications of prvalues (revisited)

Why are you doubting the language of 7.2.2? This seems pretty unambiguous that cv qualifiers are discarded on non-class, non-array prvalues, so the type T in temporary materialization is a non-const, non-volatile type. If that weren’t the case, then you wouldn’t be able to bind prvalues to non-const rvalue references. Yet it seems overwhelmingly likely … Read more

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