普通视图

发现新文章,点击刷新页面。
昨天以前Standard C++

Qt and Trivial Relocation (Part 3) -- Giuseppe D'Angelo

作者 Blog Staff
2024年7月3日 06:07

kdab.pngIn the last post of this series we started exploring how to erase an element from the middle of a vector.

Qt and Trivial Relocation (Part 3)

by Giuseppe D'Angelo

From the article:

The reference semantics backstab

Let’s start by analyzing erase()‘s behavior once more.

Do you remember our claim that the specific strategy used does not really matter; that is, that they are all equivalent? Well, not so fast! It is actually quite imprecise to say that they are all equivalent.

They may be, as long as we deal with types which have value semantics. If we instead use a type that has reference semantics, the choices are absolutely not equivalent, and will yield different outcomes. This is because the semantics of assignment for (certain) reference types are write-through: they assign through the reference (instead of rebinding the reference).

Since we are implementing erasure in terms of assignments (or swaps, which boil down to assignments), this means that the precise sequence of operations done by erase will be visible due to its side-effects; and it also means that changing the strategy will produce different outcomes!

CppCon 2023 Writing a Better std::move -- Jonathan Müller

作者 Blog Staff
2024年7月2日 02:23

cpp23-muller.pngRegistration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2024!

Lightning Talk: Writing a Better std::move

by Jonathan Müller

Summary of the talk:

std::move allows the creation of const rvalue references, which is almost always wrong. It also allows moving out of lvalue references, which can be dangerous since you don't have real ownership over them and a caller might not expect the object to disappear. Let's fix those problems using macros, reflection, and more macros.

More on Harmful Overuse of std::move -- Raymond Chen

作者 Blog Staff
2024年6月30日 05:46

RaymondChen_5in-150x150.jpgIn recent discussions around the use of std::move in C++, questions have arisen regarding its potential overuse and the compiler's treatment of its return values. Addressing concerns raised by developers like Jonathan Duncan, this article delves into the nuances of std::move, examining whether its current implementation aligns with compiler optimizations and proposing potential enhancements for more efficient code generation.

More on harmful overuse of std::move

by Raymond Chen

From the article:

Some time ago, I wrote about harmful overuse of std::move. Jonathan Duncan asked,

Is there some side-effect or other reason I can’t see return std::move(name); case isn’t possible to elide? Or is this just a case of the standards missing an opportunity and compilers being bound to obey the standards?

In the statement return std::move(name);, what the compiler sees is return f(...); where f(...) is some mysterious function call that returns an rvalue. For all it knows, you could have written return object.optional_name().value();, which is also a mysterious function call that returns an rvalue. There is nothing in the expression std::move(name) that says, “Trust me, this rvalue that I return is an rvalue of a local variable from this very function!”

Now, you might say, “Sure, the compiler doesn’t know that, but what if we made it know that?” Make the function std::move a magic function, one of the special cases where the core language is in cahoots with the standard library.

This sort of in-cahoots-ness is not unheard of. For example, the compiler has special understanding of std::launder, so that it won’t value-propagate memory values across it, and the compiler has special understanding of memory barriers, so that it won’t optimize loads and stores across them.

CppCon 2023 Linkers, Loaders and Shared Libraries in Windows, Linux, and C++ -- Ofek Shilon

作者 Blog Staff
2024年6月29日 02:20

cpp23-shilon.pngRegistration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2024!

Linkers, Loaders and Shared Libraries in Windows, Linux, and C++

by Ofek Shilon

Summary of the talk:

This talk would give a crash-intro to linkers, loaders and the layout of program binaries, and explore just enough internals to understand some observable differences in C++ builds between Linux and Windows.

We will discuss the GOT, the PLT, symbol visibility, interposition, lazy binding and more. There will be a lot of details, but also a lot of 'why's and opinions.

We will also touch/rant on what the C++ standard has to say on adjacent matters. There's a good chance you've heard before "shared libraries are outside the scope of the standard", but it doesn't mean what you think it does.

CppCon 2023 Libraries: A First Step Toward Standard C++ Dependency Mgmt--Bret Brown & Bill Hoffman

作者 Blog Staff
2024年6月28日 02:15

cpp23-brown.pngRegistration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2024!

Plenary: Libraries - A First Step Toward Standard C++ Dependency Management

by Bret Brown & Bill Hoffman

Summary of the talk:

Prebuilt libraries have existed for decades… they even predate C++! After all these years, techniques to use prebuilt libraries are still ad hoc and difficult to maintain. A root cause of this variety of techniques is the variety of things that are C++ libraries: header-only libraries, statically-linked archives, dynamically-linked binaries, and so on. The consuming projects need to build against these libraries in consistent ways or risk unproductive workflows – and potentially, even catastrophic failure in production environments. This lack of convergence creates enormous interoperability problems across broad portions of the worldwide programming ecosystem, not just the C++ parts of it.

This talk will explore the complexities of defining what is a “C++ library.” It will then present the joint work of Kitware, Bloomberg, and others toward a preliminary design for creating initial standards for dependency management in C++ – metadata files to describe prebuilt libraries. A roadmap for maturing the design will also be shared, including proposing a standard definition for C++ libraries, building on previous proposals such as P1313: Package Specification (https://wg21.link/P1313).

This talk is intended for anyone who produces, maintains, or consumes C++ libraries. Special knowledge of C++ tooling, build systems, or package managers is not required.

Qt and Trivial Relocation (Part 2) -- Giuseppe D'Angelo

作者 Blog Staff
2024年6月26日 06:09

kdab.pngIn this installment we are going to explore the relationships between trivial relocation and move assignments.

Qt and Trivial Relocation (Part 2)

by Giuseppe D'Angelo

From the article:

Last time we started our investigation of trivial relocation by considering an important use-case: reallocating a vector. This happens when a vector reaches its capacity, but more storage is needed.

Let’s now consider a different operation: erasing an element from the middle of a QVector.

How do we go about it?

isocpp-dangelo.png

CppCon 2023 Let's Fix Sparse Linear Algebra with C++. It'll Be Fun and Easy! -- Benjamin Brock

作者 Blog Staff
2024年6月25日 02:12

cpp23-brock.pngRegistration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2024!

Lightning Talk: Let's Fix Sparse Linear Algebra with C++. It'll Be Fun and Easy!

by Benjamin Brock

Summary of the talk:

Sparse linear algebra is hard.  There are a large variety of different sparse linear algebra formats, and they all require obtuse index arithmetic in order to use.  But what if we could fix this?  In this talk, I'll present an idea for "fixing sparse linear algebra" using customization points, the ranges library, and high-level multi-dimensional iteration.

CppCon 2023 Implementing Coroutines Using C++17 -- Alon Wolf

作者 Blog Staff
2024年6月23日 02:09

cpp23-wolf.pngRegistration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2024!

Lightning Talk: Implementing Coroutines Using C++17

by Alon Wolf 

Summary of the talk:

In this lightning talk, we will explore the journey of implementing coroutines in C++17 before they were added to the language in C++20.

The implementation uses macros, template metaprogramming, assembly functions, and more that resulting in working coroutines despite somewhat "horrible" code.

Discover how local variables within the coroutine body were leveraged to calculate frame sizes and ensure correct variable lifetimes during suspension, resumption, and destruction.

Pulling a Single Item From a C++ Parameter Pack by its Index -- Raymond Chen

作者 Blog Staff
2024年6月22日 05:22

RaymondChen_5in-150x150.jpgThis article explores techniques to access specific elements within a C++ parameter pack by index. It delves into the use of std::tie for creating a tuple of lvalue references and explains how std::forward_as_tuple can preserve the original reference categories of the parameters. Additionally, it highlights a proposed feature in C++26, Pack Indexing, which aims to simplify this process significantly.

Pulling a Single Item From a C++ Parameter Pack by its Index

by Raymond Chen

From the article:

Suppose you have a C++ parameter pack and you want to pluck out an item from it by index.

template<int index, typename...Args>
void example(Args&&... args)
{
    // how do I access the index'th args parameter?
}

One solution is to use std::tie:

template<int index, typename...Args>
void example(Args&&... args)
{
    auto& arg = std::get<index>(
        std::tie(args...));
}

CppCon 2023 C++ Memory Model: from C++11 to C++23 -- Alex Dathskovsky

作者 Blog Staff
2024年6月21日 02:05

cpp23-dathskovsky.pngRegistration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2024!

C++ Memory Model: from C++11 to C++23

by Alex Dathskovsky

Summary of the talk:

In the realm of C++ development, threading and memory management play a crucial role in crafting highly parallel and optimized programs. However, the absence of a memory model in C++98 posed challenges. Thankfully, with the advent of C++11, significant changes were introduced, including the introduction of a memory model, which brought forth a plethora of new and exciting tools for developers to leverage. This talk aims to delve into the realm of the C++ memory model, showcasing the arsenal of tools at our disposal. Attendees will gain insights into how CPUs and compilers optimize code and understand the criticality of adhering to the memory model correctly. Practical guidelines on utilizing these tools effectively will also be explored.

Throughout the talk, we will illustrate practical examples and share best practices for utilizing the diverse set of tools now available to us. From atomic operations to memory barriers, we will explore the range of techniques that allow us to develop robust and thread-safe code.

This talk will also illustrate the newer tools from newer C++ standards like JThread and so this talk will show how memory model is used and how it advanced since C++11.

CppCon 2023 Help! My Expression Template Type Names Are Too Long! -- Braden Ganetsky

作者 Blog Staff
2024年6月19日 02:02

cpp23-ganetsky.pngRegistration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2024!

Lightning Talk: Help! My Expression Template Type Names Are Too Long!

by Braden Ganetsky 

Summary of the talk:

Even the name of this talk is too long! If we're ever working with expression templates, we can easily make type names long enough to slow down compilation time. Suddenly our "zero-overhead" expression templates start giving a large compile time overhead. I'll show off a C++20 trick to fix this problem.

Reminder: CppCon 2024 Early Bird ends on Friday

作者 Blog Staff
2024年6月18日 06:22

The opening keynote of CppCon 2024 is just 89 days away!

If you're interested in savings, the Early Bird discount for on-line and on-site tickets is available until this Friday, June 21. After that tickets will still be available right up to the conference, but at the full ticket price.

To register for CppCon 2024 with the Early Bird discount, click this link this week.

For details of on-line and on-site tickets, see the Registration page which includes information about student registration discounts, group rates, the CppCon Academy (extra pre- and post-conference classes by world-renowned instructors), the diversity dinner, the "Meet the Presenters" banquet, and much more!

Why Can’t I Find the Injected Name of a Templated Class’s Templated Base Class? -- Raymond Chen

作者 Blog Staff
2024年6月18日 02:29

RaymondChen_5in-150x150.jpgSome time ago, I wrote about how injected class names were the C++ feature you didn’t even realize that you were using. Injected class names let you use the plain name for the class being defined without needing to fully qualify it with namespaces and template parameters. Furthermore, injected class names are public and can be inherited.

“But wait, I’m trying to use the injected class name of my base class, but the compiler won’t accept it.”

Why Can’t I Find the Injected Name of a Templated Class’s Templated Base Class?

by Raymond Chen

From the article:

Some time ago, I wrote about how injected class names were the C++ feature you didn’t even realize that you were using. Injected class names let you use the plain name for the class being defined without needing to fully qualify it with namespaces and template parameters. Furthermore, injected class names are public and can be inherited.

“But wait, I’m trying to use the injected class name of my base class, but the compiler won’t accept it.”

template<typename T>
struct Base
{
    Base(T value);
};

template<typename T>
struct Derived : Base<T>
{
    Derived(T value) : Base(value) {}
};

This generates a compiler error.

 

CppCon 2023 Filling the Bucket: Reading Code, C++ Code Interviews & Exams -- Amir Kirsh

作者 Blog Staff
2024年6月17日 01:58

cpp23-kirsh.pngRegistration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2024!

Lightning Talk: Filling the Bucket: Reading Code, C++ Code Interviews & Exams

by Amir Kirsh 

Summary of the talk:

We are going to review and practice a reading code challenge. Reading code skills are quite important, maybe even more than writing code. So let's dive together into filling the bucket code reading challenge!

CppCon 2023 Back to Basics: The Rule of Five in C++ -- Andre Kostur

作者 Blog Staff
2024年6月14日 02:32

cpp23-kostur.pngRegistration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2024!

Back to Basics: The Rule of Five in C++

by Andre Kostur

Summary of the talk:

Designing a class to behave correctly when copied and moved takes a lot of thought. The Core Guidelines provide guidance to streamline that work. In this talk we are going to look at the Core Guideline known as "the Rule of Five", how it came about, and is there anything better.

An Informal Comparison of the Three Major Implementations of std::string -- Raymond Chen

作者 Blog Staff
2024年6月13日 02:39

RaymondChen_5in-150x150.jpgWe saw some time ago that the three major implementations of std::string are all quite different...

An informal comparison of the three major implementations of std::string

by Raymond Chen

From the article:

In the original version of this article, I got the sense of the “small/large” bit backward in the clang implementation. This in turn led to redoing the code generation and new code golfing results.

We’ll compare these versions based on the complexity of some commonly-used operations.

CppCon 2023 Exceptionally Bad: The Misuse of Exceptions in C++ & How to Do Better -- Peter Muldoon

作者 Blog Staff
2024年6月12日 02:33

cpp23-muldoon.pngRegistration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2024!

Exceptionally Bad: The Misuse of Exceptions in C++ & How to Do Better

by Peter Muldoon

Summary of the talk:

Exceptions were originally heralded as a new modern way to handle errors. However the C++ community is split as to whether exceptions are useful or should be banned outright. It has not helped the pro-exception lobby that in their enthusiasm to embrace exceptions, a lot of code has been written that puts exceptions in a bad light.

In this talk, We will present the original intent/history of exceptions and a brief overview of how exception mechanics work and how they circumvent the usual stack return mechanism to set the stage. we will then examine the philosophy of using exceptions and then the many cases of exception misuse including resource management, retries, hierarchies, data passing and control flow to name but a few.

For each case, we will then suggest better ways to handle each specific situation. In many cases, exceptions are often dropped in favor of some other more appropriate paradigm.
Finally, we will introduce situations that can truly benefit from exceptions and what a model exception class might look like.

Adding State to the Update Notification Pattern, Part 7 -- Raymond Chen

作者 Blog Staff
2024年6月11日 06:45

RaymondChen_5in-150x150.jpgLast time, we refined our change counter-based stateful but coalescing update notification. This version still relies on a UI thread to do two things: (1) make the final final change counter check and the subsequent callback atomic, and (2) to serialize the callbacks.

Adding State to the Update Notification Pattern, Part 7

by Raymond Chen

From the article:

If we don’t have a UI thread, then we open a race condition.

 
class EditControl
{
    ⟦ ... existing class members ... ⟧

    std::atomic<unsigned> m_latestId;
};

winrt::fire_and_forget
EditControl::TextChanged(std::string text)
{
    auto lifetime = get_strong();

    auto id = m_latestId.fetch_add(1, std::memory_order_relaxed);

    co_await winrt::resume_background();

    if (!IsLatestId(id))) co_return;

    std::vector<std::string> matches;
    for (auto&& candidate : FindCandidates(text)) {
        if (candidate.Verify()) {
            matches.push_back(candidate.Text());
        }
        if (!IsLatestId(id))) co_return;
    }

    // co_await winrt::resume_foreground(Dispatcher());

    if (!IsLatestId(id))) co_return;

    SetAutocomplete(matches);
}

CppCon 2023 You Should Use Address Sanitizer -- Brody Holden

作者 Blog Staff
2024年6月9日 01:43

cpp23-holden.pngRegistration is now open for CppCon 2024! The conference starts on September 15 and will be held in person in Aurora, CO. To whet your appetite for this year’s conference, we’re posting videos of some of the top-rated talks from last year's conference. Here’s another CppCon talk video we hope you will enjoy – and why not register today for CppCon 2024!

Lightning Talk: You Should Use AddressSanitizer

by Brody Holden 

Summary of the talk:

This talk aims to get you, yes you, to use Address Sanitizer. ASan will detect various memory errors and is worth your time.

❌
❌