News

How Can I Prevent Myself From Using a Parameter After I’ve Extracted All Value? -- Raymond Chen

Imagine you have a function parameter that you want to protect from direct access, ensuring that all future interactions occur through a wrapper or transformation. This situation often arises in scenarios like implementing a logging wrapper for a class. In this discussion, we'll explore a clever technique known as "hide_name" to achieve this goal, allowing you to enforce the use of the wrapper and prevent direct access to the parameter.

How Can I Prevent Myself From Using a Parameter After I’ve Extracted All Value From It?

By Raymond Chen

From the article:

Suppose you have a function that takes a parameter that you want to transform in some way, and you want to require that all future access to the parameter be done through the transformed version. One example is a wrapper class that does logging.¹

struct WidgetRefWrapper
{
    WidgetRefWrapper(
        Widget& widget,
        Logger& logger) :
    m_widget(widget), m_logger(logger) {}
    
    void Toggle() try
    {
        m_logger.Log("Toggling the widget");
        m_widget.Toggle();
        m_logger.Log("Toggled the widget");
    } catch (...) {
        m_logger.Log("Exception while toggling the widget");
        throw;
    }

private:
    Widget& m_widget;
    Logger& m_logger;
};

void DoSomething(Widget& widget)
{
    Logger& logger = GetCurrentLogger();
    WidgetWrapper wrapper(widget, logger);

    // Do not use the widget directly!
    // Always use the wrapper!

    if (needs_toggling) {
        wrapper.Toggle();
    }
}

You want that “Do not use the widget directly!” comment to have some teeth. Can you “poison” the widget parameter so it cannot be used any more?

2023-12 Mailing Available

The 2023-12 mailing of new standards papers is now available.

 

WG21 Number Title Author Document Date Mailing Date Previous Version Subgroup
N4966 St. Louis Meeting Invitation and Information Bill Seymour 2023-10-19 2023-12   All of WG21
N4967 WG21 2023-10 Admin telecon minutes Nina Ranns 2023-10-30 2023-12   All of WG21
N4970 WG21 2023-11 Kona Minutes of Meeting Nina Ranns 2023-11-28 2023-12   All of WG21
N4971 Working Draft, Programming Languages -- C++ Thomas Köppe 2023-12-17 2023-12   All of WG21
N4972 Editors' Report, Programming Languages -- C++ Thomas Köppe 2023-12-17 2023-12   All of WG21
P0447R24 Introduction of std::hive to the standard library Matt Bentley 2023-10-25 2023-12 P0447R23 SG14 Low Latency,LEWG Library Evolution,All of WG21
P0447R25 Introduction of std::hive to the standard library Matt Bentley 2023-12-03 2023-12 P0447R24 SG14 Low Latency,LEWG Library Evolution,All of WG21
P0447R26 Introduction of std::hive to the standard library Matt Bentley 2023-12-17 2023-12 P0447R25 SG14 Low Latency,LEWG Library Evolution,All of WG21
P0609R2 Attributes for Structured Bindings Aaron Ballman 2023-11-27 2023-12 P0609R1 CWG Core
P0952R2 A new specification for std::generate_canonical Thomas Köppe 2023-12-18 2023-12 P0952R1 LWG Library
P1028R6 SG14 status_code and standard error object Niall Douglas 2023-12-10 2023-12 P1028R5 LEWG Library Evolution
P1061R6 Structured Bindings can introduce a Pack Barry Revzin 2023-12-10 2023-12 P1061R5 EWG Evolution
P1068R10 Vector API for random number generation Ilya Burylov 2023-12-09 2023-12 P1068R9 LWG Library
P1673R13 A free function linear algebra interface based on the BLAS Mark Hoemmen 2023-12-18 2023-12 P1673R12 All of WG21
P1708R8 Basic Statistics Richard Dosselmann 2023-12-17 2023-12 P1708R7 SG6 Numerics,SG14 Low Latency,LEWG Library Evolution,LWG Library
P1709R4 Graph Library Phillip Ratzloff 2023-12-18 2023-12 P1709R3 SG19 Machine Learning
P1709R4 Graph Library Phillip Ratzloff 2023-12-18 2023-12 P1709R3 SG19 Machine Learning
P1928R8 std::simd - Merge data-parallel types from the Parallelism TS 2 Matthias Kretz 2023-11-09 2023-12 P1928R7 LEWG Library Evolution,LWG Library
P1967R12 #embed - a simple, scannable preprocessor-based resource acquisition method JeanHeyd Meneide 2023-12-09 2023-12 P1967R11 CWG Core
P2022R3 Rangified version of lexicographical_compare_three_way Ran Regev 2023-12-17 2023-12 P2022R2 SG9 Ranges,LEWG Library Evolution
P2264R6 Make assert() macro user friendly for C and C++ Peter Sommerlad 2023-11-10 2023-12 P2264R5 LWG Library
P2264R7 Make assert() macro user friendly for C and C++ Peter Sommerlad 2023-12-18 2023-12 P2264R6 LWG Library
P2267R1 Library Evolution Policies Inbal Levi 2023-11-22 2023-12 P2267R0 LEWG Library Evolution
P2308R1 Template parameter initialization S. Davis Herring 2023-12-18 2023-12 P2308R0 CWG Core
P2414R2 Pointer lifetime-end zap proposed solutions Paul E. McKenney 2023-12-17 2023-12 P2414R1 SG1 Concurrency and Parallelism,EWG Evolution
P2447R6 std::span over an initializer list Arthur O'Dwyer 2023-12-18 2023-12 P2447R5 LWG Library
P2481R2 Forwarding reference to specific type/template Barry Revzin 2023-12-16 2023-12 P2481R1 EWG Evolution
P2542R7 views::concat Hui Xie 2023-12-02 2023-12 P2542R6 SG9 Ranges,LEWG Library Evolution,LWG Library
P2573R1 = delete("should have a reason"); Yihe Li 2023-11-10 2023-12 P2573R0 EWG Evolution
P2642R5 Padded mdspan layouts Christian Trott 2023-12-05 2023-12 P2642R4 LEWG Library Evolution,LWG Library
P2662R3 Pack Indexing Corentin Jabot 2023-12-18 2023-12 P2662R2 CWG Core
P2663R5 Proposal to support interleaved complex values in std::simd Daniel Towner 2023-12-08 2023-12 P2663R4 LEWG Library Evolution
P2664R5 Proposal to extend std::simd with permutation API Daniel Towner 2023-10-25 2023-12 P2664R4 SG1 Concurrency and Parallelism,LEWG Library Evolution
P2717R4 Tool Introspection René Ferdinand Rivera Morell 2023-11-08 2023-12 P2717R3 SG15 Tooling
P2717R5 Tool Introspection René Ferdinand Rivera Morell 2023-11-10 2023-12 P2717R4 SG15 Tooling
P2747R1 constexpr placement new Barry Revzin 2023-12-10 2023-12 P2747R0 EWG Evolution
P2758R1 Emitting messages at compile time Barry Revzin 2023-12-09 2023-12 P2758R0 EWG Evolution,LEWG Library Evolution
P2760R1 A Plan for C++26 Ranges Barry Revzin 2023-12-14 2023-12 P2760R0 SG9 Ranges,LEWG Library Evolution
P2761R0 Slides: If structured binding (P0963R1 presentation) Zhihao Yuan 2023-11-08 2023-12   EWGI SG17: EWG Incubator
P2767R2 flat_map/flat_set omnibus Arthur O'Dwyer 2023-12-09 2023-12 P2767R1 LEWG Library Evolution,LWG Library
P2795R4 Erroneous behaviour for uninitialized reads Thomas Köppe 2023-11-10 2023-12 P2795R3 SG12 Undefined and Unspecified Behavior,SG23 Safety and Security,EWG Evolution,CWG Core,LWG Library
P2806R2 do expressions Barry Revzin 2023-11-16 2023-12 P2806R1 EWG Evolution
P2810R2 is_debugger_present is_replaceable René Ferdinand Rivera Morell 2023-12-02 2023-12 P2810R1 LEWG Library Evolution
P2810R3 is_debugger_present is_replaceable René Ferdinand Rivera Morell 2023-12-03 2023-12 P2810R2 LEWG Library Evolution
P2819R2 Add tuple protocol to complex Michael Florian Hava 2023-12-18 2023-12 P2819R1 LWG Library
P2821R5 span.at() Jarrad J. Waterloo 2023-12-18 2023-12 P2821R4 SG23 Safety and Security,LEWG Library Evolution
P2826R1 Replacement functions Gašper Ažman 2023-11-05 2023-12 P2826R0 EWG Evolution
P2827R1 Floating-point overflow and underflow in from_chars (LWG 3081) Zhihao Yuan 2023-11-20 2023-12 P2827R0 LEWG Library Evolution,LWG Library
P2830R1 constexpr type comparison Gašper Ažman 2023-11-05 2023-12 P2830R0 EWG Evolution
P2845R5 Formatting of std::filesystem::path Victor Zverovich 2023-11-24 2023-12 P2845R4 LWG Library
P2863R3 Review Annex D for C++26 Alisdair Meredith 2023-12-18 2023-12 P2863R2 EWG Evolution,LEWG Library Evolution
P2864R2 Remove Deprecated Arithmetic Conversion on Enumerations From C++26 Alisdair Meredith 2023-12-18 2023-12 P2864R1 CWG Core
P2865R4 Remove Deprecated Array Comparisons from C++26 Alisdair Meredith 2023-11-12 2023-12 P2865R3 SG22 Compatibility
P2868R3 Remove Deprecated `std::allocator` Typedef From C++26 Alisdair Meredith 2023-12-18 2023-12 P2868R2 LWG Library
P2869R3 Remove Deprecated `shared_ptr` Atomic Access APIs From C++26 Alisdair Meredith 2023-12-03 2023-12 P2869R2 LEWG Library Evolution,LWG Library
P2870R3 Remove `basic_string::reserve()` From C++26 Alisdair Meredith 2023-12-18 2023-12 P2870R2 LWG Library
P2871R3 Remove Deprecated Unicode Conversion Facets From C++26 Alisdair Meredith 2023-12-18 2023-12 P2871R2 LWG Library
P2878R6 Reference checking Jarrad J. Waterloo 2023-11-13 2023-12 P2878R5 SG23 Safety and Security
P2890R1 Contracts on lambdas Timur Doumler 2023-12-07 2023-12 P2890R0 SG21 Contracts
P2890R2 Contracts on lambdas Timur Doumler 2023-12-13 2023-12 P2890R1 SG21 Contracts
P2894R1 Constant evaluation of Contracts Timur Doumler 2023-12-07 2023-12 P2894R0 SG21 Contracts
P2900R2 Contracts for C++ Joshua Berne 2023-11-11 2023-12 P2900R1 SG21 Contracts
P2900R3 Contracts for C++ Joshua Berne 2023-12-17 2023-12 P2900R2 SG21 Contracts
P2909R3 Fix formatting of code units as integers (Dude, where's my char?) Victor Zverovich 2023-11-07 2023-12 P2909R2 LWG Library
P2909R4 Fix formatting of code units as integers (Dude, where's my char?) Victor Zverovich 2023-12-18 2023-12 P2909R3 LWG Library
P2918R2 Runtime format strings II Victor Zverovich 2023-12-18 2023-12 P2918R1 LWG Library
P2932R2 A Principled Approach to Open Design Questions for Contracts Joshua Berne 2023-11-14 2023-12 P2932R1 SG21 Contracts
P2933R1 std::simd overloads for <bit> header Daniel Towner 2023-12-08 2023-12 P2933R0 SG1 Concurrency and Parallelism,LEWG Library Evolution
P2935R4 An Attribute-Like Syntax for Contracts Joshua Berne 2023-11-05 2023-12 P2935R3 SG21 Contracts
P2952R1 auto& operator=(X&&) = default Arthur O'Dwyer 2023-12-09 2023-12 P2952R0 EWG Evolution
P2961R2 A natural syntax for Contracts Timur Doumler 2023-11-08 2023-12 P2961R1 SG21 Contracts
P2968R1 Make std::ignore a first-class object Peter Sommerlad 2023-12-12 2023-12 P2968R0 LEWG Library Evolution,LWG Library
P2968R2 Make std::ignore a first-class object Peter Sommerlad 2023-12-13 2023-12 P2968R1 LWG Library
P2969R0 Contract annotations are potentially-throwing Timur Doumler 2023-12-04 2023-12   SG21 Contracts
P2977R0 Module commands database format Ben Boeckel 2023-11-14 2023-12   SG15 Tooling
P2980R1 A motivation, scope, and plan for a quantities and units library Mateusz Pusz 2023-11-28 2023-12 P2980R0 SG6 Numerics,SG23 Safety and Security,LEWG Library Evolution
P2981R1 Improving our safety with a physical quantities and units library Mateusz Pusz 2023-11-08 2023-12 P2981R0 SG23 Safety and Security,LEWG Library Evolution
P2982R1 `std::quantity` as a numeric type Mateusz Pusz 2023-11-08 2023-12 P2982R0 SG6 Numerics,LEWG Library Evolution
P2984R1 Reconsider Redeclaring static constexpr Data Members Alisdair Meredith 2023-11-12 2023-12 P2984R0 EWG Evolution
P2996R1 Reflection for C++26 Barry Revzin 2023-12-17 2023-12 P2996R0 EWG Evolution
P2999R1 Sender Algorithm Customization Eric Niebler 2023-11-09 2023-12 P2999R0 LEWG Library Evolution
P2999R2 Sender Algorithm Customization Eric Niebler 2023-12-12 2023-12 P2999R1 LEWG Library Evolution
P2999R3 Sender Algorithm Customization Eric Niebler 2023-12-12 2023-12 P2999R2 LEWG Library Evolution
P3006R0 Launder less Antony Polukhin 2023-10-19 2023-12   SG12 Undefined and Unspecified Behavior,EWG Evolution,CWG Core
P3007R0 Return object semantics in postconditions Timur Doumler 2023-12-11 2023-12   SG21 Contracts
P3016R1 Resolve inconsistencies in begin/end for valarray and braced initializer lists Arthur O'Dwyer 2023-12-09 2023-12 P3016R0 LEWGI SG18: LEWG Incubator
P3019R1 Vocabulary Types for Composite Class Design Jonathan Coe 2023-11-08 2023-12 P3019R0 LEWG Library Evolution,LWG Library
P3019R2 Vocabulary Types for Composite Class Design Jonathan Coe 2023-11-10 2023-12 P3019R1 LEWG Library Evolution,LWG Library
P3019R3 Vocabulary Types for Composite Class Design Jonathan Coe 2023-11-20 2023-12 P3019R2 LEWG Library Evolution
P3022R1 A Boring Thread Attributes Interface David Sankel 2023-11-28 2023-12 P3022R0 LEWG Library Evolution
P3023R1 C++ Should Be C++ David Sankel 2023-11-27 2023-12 P3023R0 EWG Evolution,LEWG Library Evolution
P3024R0 Interface Directions for std::simd David Sankel 2023-11-30 2023-12   LEWG Library Evolution
P3025R0 SG14: Low Latency/Games/Embedded/Financial trading/Simulation virtual Minutes to 2023/09/12 Michael Wong 2023-10-15 2023-12   SG14 Low Latency
P3026R0 SG19: Machine Learning virtual Meeting Minutes to 2023/07/13 Michael Wong 2023-10-15 2023-12   SG19 Machine Learning
P3027R0 UFCS is a breaking change, of the absolutely worst kind Ville Voutilainen 2023-10-26 2023-12   EWG Evolution
P3028R0 An Overview of Syntax Choices for Contracts Joshua Berne 2023-11-05 2023-12   SG21 Contracts
P3029R0 Better mdspan's CTAD Hewill Kang 2023-10-23 2023-12   LEWGI SG18: LEWG Incubator
P3031R0 Resolve CWG2561: conversion function for lambda with explicit object parameter Arthur O'Dwyer 2023-11-12 2023-12   CWG Core
P3033R0 Should we import function bodies to get the better optimizations? Chuanqi Xu 2023-10-31 2023-12   SG15 Tooling
P3034R0 Module Declarations Shouldn't be Macros Michael Spencer 2023-11-10 2023-12   SG15 Tooling,EWG Evolution
P3037R0 constexpr std::shared_ptr Paul Keir 2023-11-06 2023-12   SG7 Reflection,LEWG Library Evolution
P3038R0 Concrete suggestions for initial Profiles Bjarne Stroustrup 2023-12-16 2023-12   SG23 Safety and Security
P3039R0 Automatically Generate `operator->` David Stone 2023-11-07 2023-12   EWG Evolution,LEWG Library Evolution
P3040R0 C++ Standard Library Ready Issues to be moved in Kona, Nov. 2023 Jonathan Wakely 2023-12-18 2023-12   All of WG21
P3041R0 Transitioning from "#include" World to Modules Gabriel Dos Reis 2023-11-15 2023-12   SG15 Tooling
P3042R0 Vocabulary Types for Composite Class Design Jonathan Coe 2023-11-08 2023-12   LEWG Library Evolution
P3043R0 Slides: Using variable template template without meta programming Zhihao Yuan 2023-12-11 2023-12   EWG Evolution
P3046R0 Core Language Working Group "ready" Issues for the November, 2023 meeting Jens Maurer 2023-12-18 2023-12   CWG Core
P3050R0 Optimize linalg::conjugated for noncomplex value types Mark Hoemmen 2023-11-15 2023-12   LEWG Library Evolution
P3051R0 Structured Response Files René Ferdinand Rivera Morell 2023-12-11 2023-12   SG15 Tooling
P3052R0 view_interface::at() Hewill Kang 2023-11-16 2023-12   SG9 Ranges,LEWGI SG18: LEWG Incubator,SG23 Safety and Security
P3053R0 2023-12 Library Evolution Polls Inbal Levi 2023-12-15 2023-12   All of WG21
P3055R0 Relax wording to permit relocation optimizations in the STL Arthur O'Dwyer 2023-12-17 2023-12   LEWGI SG18: LEWG Incubator
P3056R0 what ostream exception Jarrad J. Waterloo 2023-11-20 2023-12   LEWG Library Evolution
P3057R0 Two finer-grained compilation model for named modules Chuanqi Xu 2023-11-21 2023-12   SG15 Tooling
P3059R0 Making user-defined constructors of view iterators/sentinels private Hewill Kang 2023-11-30 2023-12   SG9 Ranges,LEWG Library Evolution
P3060R0 Add std::ranges::upto(n) Weile Wei 2023-11-22 2023-12   LEWG Library Evolution
P3061R0 WG21 2023-11 Kona Record of Discussion Nina Ranns 2023-11-28 2023-12   All of WG21
P3062R0 C++ Should Be C++ - Presentation David Sankel 2023-11-27 2023-12   EWG Evolution,LEWG Library Evolution
P3066R0 Allow repeating contract annotations on non-first declarations Timur Doumler 2023-12-04 2023-12   SG21 Contracts
P3070R0 Formatting enums Victor Zverovich 2023-12-14 2023-12   SG16 Unicode
P3071R0 Protection against modifications in contracts Jens Maurer 2023-12-10 2023-12   SG21 Contracts
P3071R1 Protection against modifications in contracts Jens Maurer 2023-12-17 2023-12 P3071R0 SG21 Contracts
P3072R0 Hassle-free thread attributes Zhihao Yuan 2023-12-17 2023-12   LEWG Library Evolution
P3074R0 constexpr union lifetime Barry Revzin 2023-12-15 2023-12   EWG Evolution
P3075R0 Adding an Undefined Behavior and IFNDR Annex Shafik Yaghmour 2023-12-15 2023-12   CWG Core

Striving For Better C++ Code, Part I: Data Flow Analysis Basics -- Anastasia Kazakova

Anastasia-200x200.jpgWe’ll look at the basics of data flow analysis, including how it works in general, while presenting several real-world examples where it can help you write better code.

Striving For Better C++ Code, Part I: Data Flow Analysis Basics

by Anastasia Kazakova

From the article:

All data flow inspections rely on the control-flow graph. This is a graph on which vertices are the statements in the program and edges are the control flow jumps between these statements (direct code execution, conditional jumps, loops, breaks, gotos, etc.).

For example, the control-flow graph at the right represents the function foo on the left:

basic_sample_scheme.png

How to use std::span from C++20 -- Bartlomiej Filipek

How_to_use_std_span_from_C++20.pngIn this article, we’ll look at std::span which is more generic than string_view and can help work with arbitrary contiguous collections.

How to use std::span from C++20

by Bartlomiej Filipek

From the article:

Here’s an example that illustrates the primary use case for std::span:

In traditional C (or low-level C++), you’d pass an array to a function using a pointer and a size like this:

void process_array(int* arr, std::size_t size) {
  for(std::size_t i = 0; i < size; ++i) {
    // do something with arr[i]
  }
}

std::span simplifies the above code:

void process_array(std::span<int> arr_span) {
  for(auto& elem : arr_span) {  
    // do something with elem  
  } 
} 

The need to pass a separate size variable is eliminated, making your code less error-prone and more expressive.

Enodo, Divide et Impera -- Lucian Radu Teodorescu

Enodo,_Divide_et_Impera.pngHow do you untie the knotty problem of complexity? Lucian Radu Teodorescu shows us how to divide and conquer difficult problems.

Enodo, Divide et Impera

By Lucian Radu Teodorescu

From the article:

This article aims to analyse one of the most useful techniques in software engineering: the divide et impera (Divide and Conquer) technique. And maybe the most useful one.

We define the divide et impera method as a way of breaking up a problem into smaller parts and fixing those smaller parts. This applies to recursive functions (where the phrase divide et impera is most often used), but it will also apply to the decomposition of problems. At some point, we will also discuss using abstraction as a way of applying divide et impera. Finally, we will show how to use this technique in our daily engineering activities that are not strictly related to coding.

In this article, we call divide et impera a method of approaching problems that has the following characteristics:

  • breaking the problem into sub-problems
  • solving the sub-problems independently of each other
    • occasionally, an answer to a sub-problem may render solving the rest of the sub-problems unnecessary
    • sporadically, a small amount of information passes one sub-problem to another
  • combining the results of the sub-problem solutions to form the solution to the initial problem

Live and Let Die -- Martin Janzen

Resource lifetime management can be problematic. Martin Janzen reminds us how important destructors are and when to be mindful of their limitations.

Live and Let Die

By Martin Janzen

From the article:

Most experienced C++ programmers will agree that one of the best properties of our language is the ability to manage object lifecycles using constructors and destructors.

Bjarne Stroustrup [Stroustrup19] has described ctor/dtor pairs as one of C++’s most elegant features, giving us the ability to create clean types which tidy up after themselves, with predictable performance, minimal overhead, and no need for garbage collection.

In this year’s ACCU Conference Lightning Talks, Nico Josuttis singled out destructors as (spoiler alert!) “the most important C++ feature” [Josuttis23]; and Wiktor Klonowski told a sad tale of time wasted debugging a .NET program that kept running out of ports, a fate which could have been avoided by the use of dtors [Klonowski23].

At the same conference, as well as at the recent C++ On Sea, numerous speakers talked about C++ and safety, a subject that’s been very much in the news recently [NSA22], with C++ predictably receiving a lot of flak for the ease with which one can write code containing buffer overflows, memory leaks, and of course a rich and varied choice of ways to introduce undefined behaviour (UB).

In its favour, though, C++ also provides at least one way in which we can improve safety, and reliability, greatly, by use of the powerful RAII (Resource Acquisition is Initialisation) idiom: taking ownership of a resource in the ctor, then releasing it in the dtor.