I am aware that after using std::move the variable is still valid, but in an unspecified state.
This is not a universal truth. More generally, the object that was moved from is in whatever state in which the move constructor / assignment operator left it.
The standard library does have the guarantee that you describe at minimum. But it is also possible to implement a member function for your class which doesn't abide by it, and leaves the moved from object in an invalid state. It is however a good design choice to implement move operations in the way you describe.
How to check if variable is still valid or std::move was used on it?
There is no way to do such check in general within the language.
Is there any compiler option (in clang) or any way to throw an error either during runtime or compilation?
Note that using a variable after a move is not necessarily a bug at all, but can instead be an entirely correct thing to do. Some types specify exactly the state of the moved from object (std::unique_ptr
for example) and others which have the validity guarantee can be used in ways that have no pre-conditions (such as calling container.size()
).
As such, using a moved from object is only a problem if that violates a pre-condition, which would result in undefined behaviour. Clang and other compilers have runtime sanitisers that may be able to catch some undefined behaviour. There are also many warning options and static analysers that diagnose cases where bugs are likely.
Using them is a very good idea, but you should not rely solely on them because they won't be able to find all bugs. The programmer still needs to be careful when writing the program, and needs to compare it with the rules of the language. Following common idioms such as RAII, avoiding bare owning pointers (and other resource handles) goes a long way in avoiding typical bugs.