Here is the code snippet:
#include <iostream>
int main()
{
std::cout << std::hex << 123 << std::endl;
std::cout << &std::hex << 123 << std::endl;
}
What's
&std::hex? I think it acquire the address ofstd::hex. And the address would be printed out.Is there any potential problem with this code snippet because somebody said it's bad to acquire the address of a function provided by the standard library?
CodePudding user response:
The name of a function decays into a pointer to that function. So std::cout << std::hex uses the stream inserter that takes a pointer to a function that takes an std::ios_base& and returns an std::ios_base&. The stream inserter simply calls the function.
Function pointers have another peculiar property: when you write &std::hex, it evaluates to the address of the function. Same for &&std::hex, &&&&std::hex, etc. They all mean the same thing.
