The strange behavior of Koenig Lookup

consider the following program:

    namespace NS2 {
      class base { };

      template<typename T>
      int size(T& t) {
        std::cout << "size NS2 called!" << std::endl;
        return sizeof(t);
      } 
    };

    namespace NS1 {
      class X : NS2::base { };
    }

    namespace NS3 {
      template<typename T>
      int size(T& t) {
        std::cout << "size NS3 called!" << std::endl;
        return sizeof(t) + 1;
      }

      template<typename T>
      class tmpl 
      {
      public:
        void operator()() { size(*this); }
      };
    };

int main() +{
  NS3::tmpl<NS1::X> t;
  t();
  return 0;
}

My compiler (gcc 4.3.3) does not compile the program because the size call is undefined. The NS2 namespace seems to be added to the set of associated namespaces to call size in the tmpl class. Even after reading the Koenig Lookup section of the ISI standard, I'm not sure if this behavior complies with the standard. It? Does anyone know a way around this behavior without qualifying a size call with an NS3 prefix?

Thanks in advance!

+3
source share
1 answer

ADL, , GCC : NS3 , NS1 X NS2 .

- ; , , , , SFINAE, .

( : , boost:: noncopyable "typedef noncopyable _:: noncopyable noncopyable;", boost ADL, .)

+8

Source: https://habr.com/ru/post/1717658/


All Articles