Is the void() in decltype(void()) an expression or is it a function type?

Using a hyperlinked C++ grammar, the parsing of decltype(void()) is: decltype( expression ) decltype( assignment-expression ) decltype( conditional-expression ) … lots of steps involving order of operations go here … decltype( postfix-expression ) decltype( simple-type-specifier ( expression-listopt ) ) decltype( void() ) So void() is a kind of expression here, in particular a postfix-expression. Specifically, … Read more

Can a C++ function be declared such that the return value cannot be ignored?

To summarize from other answers & comments, basically you have 3 choices: Get C++17 to be able to use [[nodiscard]] In g++ (also clang++), use compiler extensions like __wur (defined as __attribute__ ((__warn_unused_result__))), or the more portable (C++11 and up only) [[gnu::warn_unused_result]] attribute. Use runtime checks to catch the problem during unit testing If all … Read more

Switch expression with void return type

Maybe yield a Consumer of Event, so you yield something useful, the trade off is one more line for consumer.accept. Consumer<Event> consumer = switch (event.getEventType()) { case ORDER -> e -> handle((OrderEvent) e); case INVOICE -> e -> handle((InvoiceEvent) e); case PAYMENT -> e -> handle((PaymentEvent) e); }; consumer.accept(event); Continue if you concern performance Based … Read more

Why does C# not allow me to call a void method as part of the return statement?

Because it’s simply the way the language is defined. A method can use return statements to return control to its caller. In a method returning void, return statements cannot specify an expression. In a method returning non-void, return statements must include an expression that computes the return value. It’s an arbitrary decision (presumably made for … Read more

Await an async void method call for unit testing

You should avoid async void. Only use async void for event handlers. DelegateCommand is (logically) an event handler, so you can do it like this: // Use [InternalsVisibleTo] to share internal methods with the unit test project. internal async Task DoLookupCommandImpl(long idToLookUp) { IOrder order = await orderService.LookUpIdAsync(idToLookUp); // Close the search IsSearchShowing = false; … Read more