19

I know that you can use static_cast<void>, but it just seems too verbose for me, and not reflecting the original intent that I want to discard a return value, not to cast it to anything.

Recently I stumbled upon std::ignore, which can accept a value of any type, the name is clear and readable, and to me it seems fitting.

I know that the initial intent was to use std::ignore alongside with std::tie to discard any unwanted values, but I guess the original intent of static_cast was to actually cast values for some better reasons than discarding values so the compiler won't complain.

So, is it OK to use std::ignore for the purpose I described in the question?

For example:

std::ignore = std::transform(...);
13
  • 2
    Your usage looks valid. @FrançoisAndrieux see here - the first example is not related to std::tie (std::ignore = dontIgnoreMe();).
    – wohlstad
    Sep 28 at 13:42
  • 2
    @wohlstad It is a wiki after all. It's usually pretty accurate but there are no guaranties. Checking the latest standard, you can search for ignore here and (besides the stream function) it only appears as part of tuple creation. Sep 28 at 13:47
  • 4
    Regarding using static_cast<void>, this is one case where it is generally harmless to use an explicit conversion to void like (void)my_function();. Nobody can use the result, by definition, which eliminates most of the risks related to using them. And it is a relatively well known convention, so shouldn't act as a red flag or cause for confusion or concern. Sep 28 at 13:51
  • 3
    BTW, std::transform doesn't return a [[nodiscard]]. so std::ignore = can even be removed :-), no warnings expected (else we would prefix any expression by it as std::ignore = std::cout << "hello" :) ).
    – Jarod42
    Sep 28 at 13:54
  • 2
    Just use (void). Everyone understands what that's doing and it's very idiomatic at this point. Sep 29 at 10:40

2 Answers 2

18

Yes, it is okay. In fact, it is considered much better style by some people.

Never cast to (void) to ignore a [[nodiscard]] return value. If you deliberately want to discard such a result, first think hard about whether that is really a good idea (there is usually a good reason the author of the function or of the return type used [[nodiscard]] in the first place). If you still think it’s appropriate and your code reviewer agrees, use std::ignore = to turn off the warning which is simple, portable, and easy to grep.

- CppCoreGuidelines ES.48: Avoid casts

Note: Herb Sutter committed the guideline. With the std::ignore rule, you can consistently teach not to use casts, and don't have to make an exception for casting to void.

An argument against std::ignore is that it is only defined in terms of its effect in std::tie:

template<class... TTypes>
constexpr tuple<TTypes&...> tie(TTypes&... t) noexcept;

Returns: tuple<TTypes&...>(t...). When an argument in t is ignore, assigning any value to the corresponding tuple element has no effect.

- [utilities] std::tie

This is mostly a philosophical issue though. In every major standard library, std::ignore is implemented in such a way that you can do std::ignore = ... on its own, and this may soon be well-defined. See P2968: Make std::ignore a first-class object.

In the end, it's stylistic preference. You can use (void), static_cast<void>, or std::ignore. They are all acceptable, and which one to use is a matter of opinion. What matters is that you use a consistent style throughout your project, i.e. if you use std::ignore to discard results in one place, use it everywhere.

0
5

In theory, std::ignore's operator= could be implemented as:

[[nodiscard]] auto& operator=(auto&&...) const { return *this; }

So instead of suppressing compiler warnings, assigning to std::ignore could cause a warning.

And std::tuple's operator= could do something like this, to suppress the warning when std::ignore is part of a tuple:

(static_cast<void>(std::get<I>(*this) = std::get<I>(rhs)), ...);

In practice, no implementation defines std::ignore like this.

Whether it is OK to use std::ignore = depends on whether you want to rely on the fact that no implementation uses [[nodiscard]] in the definition of std::ignore. I, personally, would rather not rely on this undocumented fact.

1
  • 1
    You might as well consider a hypothetical implementation that = deletes the operator = for std::ignore, and wraps the assignment in std::tuple in an if constexpr (!std::is_same_v<decltype(std::ignore), […]>). Sep 29 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.