Sunday, August 12, 2012

ostream operator shading



#include <iostream>

struct B{
};

std::ostream& operator<<(std::ostream& o,const B& a){
  return o<<"B";
}

namespace d{
  struct D{};

  std::ostream& operator<<(std::ostream& o,const D& a){
  return o<<"D";
  }

}

namespace a{
  struct A{
  };

  struct C{};
  std::ostream& operator<<(std::ostream& o,const C& a){return o;}
}

std::ostream& operator<<(std::ostream& o,const a::A& a){
  return o<<"A";
}

namespace a{

  void f(){
    A a;
    B b;
    d::D d;
    std::cout<<b<<d<<std::endl;
  }
}


int main(){
  a::f();
}


Printing B is always fine. If the definition for << C is given inside ns a, then the definition for a is lost (B or D is fine)

No comments:

Post a Comment