I was doing Leetcode when I stumbled upon this interesting technique. Here it is in short:
#include <iostream>
#include <vector>
void print(std::vector<int>& vec)
{
for (int i : vec)
{
std::cout << i << ", ";
}
}
int main()
{
print(std::vector<int>(5) = {1,2,3,4,5});
return 0;
}
We are passing seemingly rvalue to an lvalue reference, which is usually illegal, but in this specific case, it works somehow. Is there any explanation or maybe I'm missing something?
std::vector<int>(5) = {1,2,3,4,5}itself should be flagged5is pretty pointless.