0

If I have a free function:

 enum BOM { utf8, utf16le };
 size_t length(BOM b);

And then I have a class with a member named length:

 struct foo {
     BOM m_bom;
     size_t length() { return length(m_bom); }
  };

Why does the use of length(m_bom) fail to find the free function using type dependent lookup?

I'm using VS2015, but I suspect that they've got these rules right by now, so the question is probably a language question...

Mordachai
  • 9,412
  • 6
  • 60
  • 112
  • Did you try using namespaces? – Mo Abdul-Hameed Oct 18 '16 at 21:23
  • 3
    The member function shadows, rather than overloads, the free function. Once the member function is found, the lookup is over, even if the arguments don't match. – n. m. could be an AI Oct 18 '16 at 21:27
  • 2
    The question marked as duplicate is kind of ironic, since you've obviously seen it - you left a comment there. But don't feel bad, I've done the same thing myself. – Mark Ransom Oct 18 '16 at 21:38
  • lol - not only have I seen it b4, but I still agree with myself: this behavior sucks and is a wrong choice for the language. – Mordachai Oct 19 '16 at 13:58
  • You were misled by yourself :) It isn't a wrong choice. – n. m. could be an AI Oct 19 '16 at 21:06
  • Yes, it is. type-binding lookup is far more consistent and this is a flaw in c++ - fundamentally so. Unless you can link me to a set of arguments / examples that show how it would be a worse language the other way round. – Mordachai Oct 20 '16 at 15:01

0 Answers0