An object can be instantiated through assignment operator with an initializer list.
Why can't I compare an object with an initializer list given the fact that the compiler deduces the type of the right hand operand when assigning an initializer list to an object.
#include <string>
#include <map>
class FileInfo
{
public:
FileInfo(const std::string &name) : mName(name) { }
bool operator == (const FileInfo& other) const
{
return mName == other.mName;
}
private:
std::string mName;
};
int main(int argc, char *argv[])
{
FileInfo f1 = { "f1" };
FileInfo f2 = { "f2" };
if (f1 == f2) {} // OK
if (f1 == { "f2" }) {} // C2059 syntax error: ')'
if ((f1 = { "f1" }) == f2) { } // OK
if (f1 == FileInfo{ "f2" }) {} // OK
return 0;
}
I am trying to understand if there is an objective reason why the comparison fails. Is there a compiler limitation?