llvm-project
llvm-project copied to clipboard
Reference to temporary local variable not detected
Consider this example:
#include <iostream>
#include <type_traits>
#include <vector>
class VectorHolder {
private:
std::vector< int > vec_;
public:
VectorHolder( std::vector< int > && vec ): vec_( std::move( vec ) ) {}
auto const & vec() const { return vec_; }
};
template< typename ... T >
auto packToVector( T ... args ) requires ( std::is_same_v< T, int > && ... ) {
return VectorHolder{ std::vector< int >{ args... } };
}
int main() {
for ( auto i : packToVector( 1, 2, 3, 4, 5 ).vec() ) {
std::cout << i << ' ';
}
return 0;
}
Should this bug be auto-detectable by -Wlifetime? Or it is required to annotate vec method with [[ gsl::post( lifetime, { this } ) ]] (AFAIK, this attribute is still not supported).
Unfortunately, members are partially supported at this point. Once it is fully implemented, I think this specific example should work without annotations. Thanks for reporting this!
The gsl::post annotation is also only partially supported at this point. Unfortunately, I have hard time to find time working on this lately.