When I rely on lifetime extension to assign to a class with a non trivial destructor the compiler (both gcc and clang) issue unused variable warnings. Is there anyway to get around this? https://wandbox.org/permlink/qURr4oliu90xJpqr
#include <iostream>
using std::cout;
using std::endl;
class Something {
public:
explicit Something(int a_in) : a{a_in} {}
Something() = delete;
Something(Something&&) = delete;
Something(const Something&) = delete;
Something& operator=(Something&&) = delete;
Something& operator=(const Something&) = delete;
~Something() {
cout << this->a << endl;
}
private:
int a;
};
int main() {
const auto& something = Something{1};
return 0;
}
Note that when I switch to not relying on lifetime extension, things work just fine https://wandbox.org/permlink/cJFwUDdi1YUEWllq
I even tried manually defining all the constructors and then deleting them with a templated static_assert
so that it only fires when those constructors are called https://wandbox.org/permlink/fjHJRKG9YW6VGOFb
#include <iostream>
using std::cout;
using std::endl;
template <typename Type>
constexpr auto definitely_false = false;
template <typename T = void>
class Something {
public:
explicit Something(int a_in) : a{a_in} {}
Something() { static_assert(definitely_false<T>, ""); }
Something(Something&&) { static_assert(definitely_false<T>, ""); }
Something(const Something&) { static_assert(definitely_false<T>, ""); }
Something& operator=(Something&&) { static_assert(definitely_false<T>, ""); }
Something& operator=(const Something&) { static_assert(definitely_false<T>, ""); }
~Something() {
cout << this->a << endl;
}
private:
int a;
};
int main() {
const auto& something = Something<>{1};
return 0;
}
Let's say just for the language lawyer tag's sake that casting to void is not an option. Can I do something with the constructors/destructors that will help silence this warning?
glDebugMessageInsert
to easily orient in theapitrace
output). Create an instance at the beginning of a function, and it'll automatically trace any normal return—be it an earlyreturn
, athrow
from some nested function call, or reaching the end of the function. Though I didn't use a reference, just created an automatic object, but the unused variable warning is also a problem. – Vivienvivienereturn
. That said, I guess I would probably create a template classFinally<>
or similar and pass its constructor a lambda (allows for catching local variables as well) if I had that problem. I would then search for some__attribute__(())
voodo to squelch such warnings for that class. Or maybe some macro rites to add some "use" to the object. I don't know, it's tricky. – Myrick