Why this code cannot compile?
#include <string>
#include <iostream>
struct Foo
{
operator std::string () const
{
return std::string("Hello world !");
}
};
int main(void)
{
Foo f;
std::cout << f << "\n";
}
while this one does:
#include <string>
#include <iostream>
int main(void)
{
std::cout << std::string("Hello world !") << "\n";
}
and this one too:
#include <string>
#include <iostream>
struct Bar
{
operator int ()
{
return 42;
}
};
int main(void)
{
Bar b;
std::cout << b << "\n";
}
I thought this was the same thing, because of operator std::string
, but g++ does not compile neither clang++.