Fluent::Programmer

    Home Blog About
  • Home
  • Blog
  • About
  • c++
  • beautiful-code-series
  • linux
  • algorithms
  • assembly-language
  • c-programming
  • network-programming

Beautiful code #3 - [[no_unique_address]] - sophisticated and simple C++20 featuređź‘‹

  • Fluent Programmer
  • February 12, 2023
  •   2 min read

Quick summary ↬  [[no_unique_address]] is a C++ 20 feature that helps in saving memory while declaring an empty struct or class and creating object for it in some other place.

Beautiful code #3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
//======empty member=========//
#ifndef CAPSY_EMPTY_MEMBER
# if defined(__has_cpp_attribute) && __has_cpp_attribute(no_unique_address) 
#   define CAPSY_HAS_EMPTY_MEMBER 1
#   define CAPSY_EMPTY_MEMBER [[no_unique_address]]
# else
#   define CAPSY_HAS_EMPTY_MEMBER 0
#   define CAPSY_EMPTY_MEMBER
# endif 
#endif

namespace capsy {
template <typename Encoding, typename Iterator, typename Sentinel = Iterator>
class range_input {
    public:
        using encoding = Encoding;
        using char_type = encoding::char_type;
    private:
        Iterator    _cur;
        CAPSY_EMPTY_MEMBER Sentinel _end;
};
}

Beautiful code #3 Explanation

The above C++ code snippet is very simple but very beautiful one. This code snippet is useful while designing a library that could be installed and used in varieties of hardwares. We check whether a feature is available in the version of C++ installed and then we apply it. If it is not available the macro won’t pass the command in the codebase where it is called.

defined(__has_cpp_attribute) && __has_cpp_attribute(no_unique_address) - This checks whether __has_cpp_attribute is defined and then uses it to check whether the standard library installed in the system where the library is running has a particular feature. If [[no_unique_address]] code is supported then the macro applies it in all the code where the macro is called.

[[no_unique_address]] is a C++ 20 feature that is used infront of a struct or class which is empty. If used we could save 1 byte whenever an object is created for another class that instantiate the object for the empty struct or class. Not allocating memory for no reason is much needed functionality to develop a well optimized library.

In the above example Sentinel is declared with [[no_unique_address]].

About The Author

Fluentprogrammer doesn't need coffee to program. They run on pure caffeine and lines of code.

Email Newsletter

Table of Contents

  • Beautiful code #3
  • Beautiful code #3 Explanation
  • C++
  • Systems & Network
  • C programming
  • Beautiful code
  • Design patterns
  • Linux
  • Open Source
  • Algorithms
  • Data Structures
  • System design
  • Distributed systems
  • Kernel
  • Assembly language
  • Hardware
  • Ultra Low Latency
  • Inspiration

Unhealthy love with dark corners of C++

Founded by an engineer to help engineers. 2021–2023.

Fluentprogrammer.com is a brand name managed by Abyan, Inc.

  • About us
  • Privacy policy