locked
Doubt while understanding issue with Argument-dependent lookup RRS feed

  • Question

  • Hi,

    While understaing ADL working in c++ I  got sample code at http://en.cppreference.com/w/cpp/language/adl

    I modified the sample example present in above link as ,

    int main()

    {

     int a = 1234;
     cout << a;
     cout.operator<<(a);
     operator<<(std::cout, a);

    return 0;

    }

    After compiling it gives error at "operator<<(std::cout, a);" , why it fails to deduce the type of "a" as integer and call function like ostream& operaot<<(int &) .

    Please help me to understand it.

    Thanks,

    Jafar


    Thanks, Jafar

    Thursday, May 7, 2015 1:25 PM

Answers

  • On 5/7/2015 11:03 AM, "Jafar Kumarkiri" wrote:

    Then how does the statment

    operator<<(std::cout, "Hello,World");

    correctly get executed but not  operator<<(std::cout, 10);

    There are many different flavors of operator<<. Some are members of ostream, others are non-member functions taking ostream& as the first parameter. In particular, ostream::operator<<(int) is a member function, std::operator<<(ostream&, const char*) is a non-member.

    When you use operator notation, as in "cout << someObject", the compiler looks up both kinds and performs overload resolution on all of them. But when you spell out operator<<() explicitly, it's a regular function call - you now have to match the form of the call to the way the particular function you are trying to call was declared.


    Igor Tandetnik
    Thursday, May 7, 2015 6:40 PM

All replies

  • On 5/7/2015 9:25 AM, "Jafar Kumarkiri" wrote:

    After compiling it gives error at "operator<<(std::cout, a);" , why it fails to deduce the type of "a" as integer and call function like ostream& operaot<<(int &) .

    If you have a class C with a member function C::f(), and an object obj of that class, you can call this member function as "obj.f()", but not as "f(obj)". Similarly, you can call a member function ostream::operator<<(int) as cout.operator<<(a), but not as operator<<(cout, a).

    This has nothing to do with name lookup, argument-dependent or otherwise.


    Igor Tandetnik
    Thursday, May 7, 2015 2:25 PM
  • Then how does the statment

    operator<<(std::cout, "Hello,World");

    correctly get executed but not  operator<<(std::cout, 10);

    What you say , please comment.


    Thanks, Jafar

    Thursday, May 7, 2015 3:03 PM
  • On 5/7/2015 11:03 AM, "Jafar Kumarkiri" wrote:

    Then how does the statment

    operator<<(std::cout, "Hello,World");

    correctly get executed but not  operator<<(std::cout, 10);

    There are many different flavors of operator<<. Some are members of ostream, others are non-member functions taking ostream& as the first parameter. In particular, ostream::operator<<(int) is a member function, std::operator<<(ostream&, const char*) is a non-member.

    When you use operator notation, as in "cout << someObject", the compiler looks up both kinds and performs overload resolution on all of them. But when you spell out operator<<() explicitly, it's a regular function call - you now have to match the form of the call to the way the particular function you are trying to call was declared.


    Igor Tandetnik
    Thursday, May 7, 2015 6:40 PM
  • Thanks Igor :)

    Thanks, Jafar

    Friday, May 8, 2015 5:11 AM