C++17 Purpose of std::from_chars and std::to_chars?

std::stringstream is the heavyweight champion. It takes into consideration things like the the stream’s imbued locale, and its functionality involves things like constructing a sentry object for the duration of the formatted operation, in order to deal with exception-related issues. Formatted input and output operations in the C++ libraries have some reputation for being heavyweight, … Read more

Why does moving std::optional not reset state

Unless otherwise specified, a moved-from object of class type is left in a valid but unspecified state. Not necessarily a “reset state”, and definitely not “invalidated”. For primitive types , moving is the same as copying, i.e. the source is unchanged. The defaulted move-constructor for a class type with primitive members will move each member, … Read more

Correctly propagating a `decltype(auto)` variable from a function

That’s the simplest and most clear way to write it: template <typename F> auto invoke_log_return(F&& f) { auto result = f(); std::printf(” …logging here… %s\n”, result.foo()); return result; } The GCC gets the right (no needless copies or moves) expected result: s() in main prvalue s() …logging here… Foo! lvalue s(const s&) …logging here… Foo! … Read more

Do const references in structured bindings extend the lifetime of the decomposed object?

Yes. The trick is to realize that despite the appearance, the portion of a structured binding declaration before the [ doesn’t apply to the names in the identifier-list. They apply instead to the variable introduced implicitly by the declaration. [dcl.struct.bind]/1: First, a variable with a unique name e is introduced. If the assignment-expression in the … Read more

Should reading negative into unsigned fail via std::cin (gcc, clang disagree)?

I think that both are wrong in C++171 and that the expected output should be: 4294967295 0 While the returned value is correct for the latest versions of both compilers, I think that the ios_­base​::​failbit should be set, but I also think there is a confusion about the notion of field to be converted in … Read more

Compile-time reflection in C++1z? [closed]

Reflection use cases are outlined in N3814: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3814.html The general view is that we will do it as an extension of the Type Traits library as exemplified by N3815: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3815.html There are two parts to reflection. The first is introspection. Taking an entity and querying constant values about it. The second is reification, which is … Read more

Does struct with reference member have unique object representation?

Firstly, references are not objects. Objects are specified in [intro.object] and references in [dcl.ref]. Subobjects are objects ([intro.object]). Therefore reference members are not subobjects and therefore a class containing only reference members (and no bases) has no subobjects (even though it has data members). [meta.unary.prop] The predicate condition for a template specialization has_­unique_­object_­representations shall be … Read more

C++17 lambda capture *this

How is it useful? It’s useful when you need a copy of *this – for example, when *this itself is no longer valid by the time the lambda is evaluated. How is it different from capturing this? It makes a copy of the object, so that when the lambda is evaluated, its this pointer refers … Read more