0

There are multiple questions here & here regarding type-casting to void.

My question is, what's the purpose of void casting (void)dummy_pack; in en.cppreference.com/w/cpp/language/fold? Is it to explicitly indicate the value of dummy_pack is ignored and to suppress compiler warning of unused variable? If it is supposed to be ignored, why dummy_pack and std::index_sequence<dummy_pack...>is used in bswap_impl?

#include <climits>
#include <concepts>
#include <cstdint>
#include <iostream>
#include <type_traits>
#include <utility>
#include <vector>
 
template<typename... Args>
void printer(Args&&... args)
{
    (std::cout << ... << args) << '\n';
}
 
template<typename T, typename... Args>
void push_back_vec(std::vector<T>& v, Args&&... args)
{
    static_assert((std::is_constructible_v<T, Args&&> && ...));
    (v.push_back(std::forward<Args>(args)), ...);
}
 
template<class T, std::size_t... dummy_pack>
constexpr T bswap_impl(T i, std::index_sequence<dummy_pack...>)
{
    T low_byte_mask = (unsigned char)-1;
    T ret{};
    ([&]
    {
        (void)dummy_pack;
        ret <<= CHAR_BIT;
        ret |= i & low_byte_mask;
        i >>= CHAR_BIT;
    }(), ...);
    return ret;
}
 
constexpr auto bswap(std::unsigned_integral auto i)
{
    return bswap_impl(i, std::make_index_sequence<sizeof(i)>{});
}
 
int main()
{
    printer(1, 2, 3, "abc");
 
    std::vector<int> v;
    push_back_vec(v, 6, 2, 45, 12);
    push_back_vec(v, 1, 2, 9);
    for (int i : v) std::cout << i << ' ';
 
    static_assert(bswap<std::uint16_t>(0x1234u) == 
                                       0x3412u);
    static_assert(bswap<std::uint64_t>(0x0123456789abcdefull) ==
                                       0xefcdab8967452301ULL);
}
cpp
  • 265
  • 1
  • 6
  • Casting to `void` is a minimal way to "use" the value, and avoid a warning for an unused value. The value *is* used (in a way) by the `...`, but perhaps some compiler will warn anyway? – BoP Apr 27 '23 at 08:45
  • You are right @BoP but my question is more about if dummy_pack is meant to be ignored/unused, what's the purpose of having dummy_pack & index_sequence in above example? I have updated my question to make it more accurate. Thanks – cpp Apr 27 '23 at 10:04
  • 1
    The index_sequence decides how many times the lambda is run in the fold expression. It is written so it doesn't care about the values themselves, just the number of values (= number of bytes in T). (And there are less convoluted ways to do this, considering that `sizeof(i)` will be 2, 4, or 8). – BoP Apr 27 '23 at 11:23

1 Answers1

1

The

(void)dummy_pack;

really only means: "use the variable, but discard the value". But look what happens, if the statement were removed from the expression:

    ([&]
    {
        ret <<= CHAR_BIT;
        ret |= i & low_byte_mask;
        i >>= CHAR_BIT;
    }(), ...);

Now, the expression intends to be a fold expression, but, lo and behold, there is no pack to be expanded anywhere inside. Hence, this is invalid.

Therefore, the only purpose of (void)dummy_pack is to provide a pack to be expanded for the fold expression; the remainder of the fold expression is only concerned with the side-effects on the captured local variables.

j6t
  • 9,150
  • 1
  • 15
  • 35