The following code gives me a warning about passing a temporary as a parameter to a function which takes a reference:
struct TempObject
{
typedef TempObject& reference;
const int First, Second;
TempObject(int first, int second) : First(first), Second(second) {}
};
void Function(const TempObject::reference ref)
{
std::cout<< ref.First << ref.Second;
}
int main(int argc, char *argv[])
{
Function(TempObject(1, 2)); // warning is given here
}
This confuses me, because Function
is in fact accepting a const TempObject::reference
. Changing the definition of TempObject::reference
to typedef const TempObject& reference
causes the compiler to no longer emit the warning.
Why is there a difference? I found this behavior with Microsoft's and Intel's compilers.