Fluent::Programmer

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

Beautiful code #1 - Using define, templates and r-value reference 👋

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

Quick summary ↬  In this short article, I have posted an example that does the reinterpret_cast on an integer and convert it to char pointer and then the value pointed by the char pointer is converted to r-value reference all in a macro.

Beautiful code #1

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
#include <iostream>
#include <typeinfo>

namespace capsy::_detail 
{
template <typename T>
using add_rvalue_ref = T&&;
} // namespace capsy::_detail


#define CAPSY_DECLVAL(...) reinterpret_cast<::capsy::_detail::add_rvalue_ref<__VA_ARGS__>>(*reinterpret_cast<char*>(1024))

int main() {
    std::cout << typeid(decltype(CAPSY_DECLVAL(int))).name() << std::endl;
    return 0;
}

Beautiful code #1 explanation

Let me explain the macro first.

1
#define CAPSY_DECLVAL(...) reinterpret_cast<::capsy::_detail::add_rvalue_ref<__VA_ARGS__>>(*reinterpret_cast<char*>(1024))

We have declared a macro called CAPSY_DECLVAL which can accept any number of variables (note the …). In the macro definition we have double reinterpret_cast. The outcome of the far right reinterpret_cast is converting the integer 1024 to char*. This is very dangerous conversion but that is how reinterpret_cast works. Once it is converted to char* the value pointed by the char* is passed to the parent level reinterpret_cast. The sub code is *reinterpret_cast<char*>(1024).Now the __VA_ARGS__ has the value int which is passed in the main function as an argument to the macro.

Let’s move one step to the left again and see that a variable add_rvalue_ref is called that is just a r-value reference T&&. So the interpretation in our example is int&&. Now let’s write the whole macro after applying the template parameter as shown below:

1
reinterpret_cast<int&&>(*reinterpret_cast<char*>(1024))

In short, an integer converted to char* and then the value pointed by the char* is again converted to int&& and that is all defined inside the macro CAPSY_DECLVAL which accepts variadic parameters. Inside the main function we just print the data type of the variable we arrive at using the code typeid(decltype(CAPSY_DECLVAL(int))).name(). Isn’t that beautiful?

I will write series of beautiful code and I hope to come up with 500 snippets of beautiful C++ code in 2023.

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 #1
  • Beautiful code #1 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