Reputation: 7539
In the following code, a array with repeated elements is created using a fold expression:
#include <array>
#include <utility>
template <std::size_t N>
class A {
int m_x;
public:
static constexpr std::size_t size = N;
A(int x) : m_x{x} { }
int f() const noexcept { return m_x; }
int g() const noexcept { return m_x * m_x; }
};
template <typename U, std::size_t... I>
auto get_array_impl(const U& m, std::index_sequence<I...>) {
return std::array<int, m.size + 1>{ m.g(), (I, m.f())... };
}
template <typename U, std::size_t... I>
auto get_array(const U& m) {
return get_array_impl(m, std::make_index_sequence<m.size>{});
}
int main()
{
A<3> x{8};
auto a = get_array(x);
return 0;
}
Check it live on Coliru.
Compiling the code gives a warning:
main.cpp:18:50: warning: left operand of comma operator has no effect [-Wunused-value]
18 | return std::array<int, m.size + 1>{ m.g(), (I, m.f())... };
While it's clear why the warning is created (I is practically discarded), it is also clear that this is not an issue, since the code does not really need the I-values, but rather uses the variadic expansion to repeat m.f()
.
How can I suppress the warning while still compiling with -Wall
? Is there some variation of the syntax that gives the same results without raising the warning?
Upvotes: 0
Views: 197
Reputation: 11
You can avoid compiler warnings by using statements that block specific warnings, such as
#pragma GCC diagnostic ignored "-Wunused-variable"
Upvotes: 0