I'm trying to determine whether a C++ function can be declared in such a way that the return value cannot be ignored (ideally detected at compile time). I tried to declare a class with a private
(or in C++11, delete
d) operator void()
to try to catch the implicit conversion to void when a return value is unused.
Here's an example program:
class Unignorable {
operator void();
};
Unignorable foo()
{
return Unignorable();
}
int main()
{
foo();
return 0;
}
Unfortunately, my compiler (clang-703.0.31) says:
test.cpp:2:5: warning: conversion function converting 'Unignorable' to 'void' will never be used
operator void();
^
and doesn't raise any error or warning on the call to foo()
. So, that won't work. Is there any other way to do this? Answers specific to C++11 or C++14 or later would be fine.