Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ Namespace scope bug / feature

C++ Namespace scope bug / feature

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anthony Mushrow
    wrote on last edited by
    #1

    Just ran into something I thought was odd today, if you had the following

    namespace A
    {
    enum Things
    {
    Apple,
    Orange,
    HamSandwich,
    RocketPoweredElephant
    };

    void Function(Things thing)
    {
    
    }
    

    }

    namespace B
    {
    void Function(A::Things thing)
    {

    }
    
    void Ambiguous()
    {
    	Function(A::Orange); //Error - Ambiguous call
    }
    

    }

    void main()
    {
    Function(A::Orange); //Will call A::Function
    }

    Because one of the parameters is type contained within namespace A, A::Function is being brought into scope, causing an error for the function Ambiguous. Is this part of the spec or is it some compiler (VS2012) jiggery pokery?

    L A 2 Replies Last reply
    0
    • A Anthony Mushrow

      Just ran into something I thought was odd today, if you had the following

      namespace A
      {
      enum Things
      {
      Apple,
      Orange,
      HamSandwich,
      RocketPoweredElephant
      };

      void Function(Things thing)
      {
      
      }
      

      }

      namespace B
      {
      void Function(A::Things thing)
      {

      }
      
      void Ambiguous()
      {
      	Function(A::Orange); //Error - Ambiguous call
      }
      

      }

      void main()
      {
      Function(A::Orange); //Will call A::Function
      }

      Because one of the parameters is type contained within namespace A, A::Function is being brought into scope, causing an error for the function Ambiguous. Is this part of the spec or is it some compiler (VS2012) jiggery pokery?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      I assume it's part of the spec, based on the following error messages:

      AConsole.cpp
      1>c:\users\richard\documents\visual studio 2010\projects.c++\aconsole\aconsole\aconsole.cpp(98): error C2668: 'B::Function' : ambiguous call to overloaded function
      1> c:\users\richard\documents\visual studio 2010\projects.c++\aconsole\aconsole\aconsole.cpp(91): could be 'void B::Function(A::Things)'
      1> c:\users\richard\documents\visual studio 2010\projects.c++\aconsole\aconsole\aconsole.cpp(83): or 'void A::Function(A::Things)' [found using argument-dependent lookup]
      1> while trying to match the argument list '(A::Things)'

      1 Reply Last reply
      0
      • A Anthony Mushrow

        Just ran into something I thought was odd today, if you had the following

        namespace A
        {
        enum Things
        {
        Apple,
        Orange,
        HamSandwich,
        RocketPoweredElephant
        };

        void Function(Things thing)
        {
        
        }
        

        }

        namespace B
        {
        void Function(A::Things thing)
        {

        }
        
        void Ambiguous()
        {
        	Function(A::Orange); //Error - Ambiguous call
        }
        

        }

        void main()
        {
        Function(A::Orange); //Will call A::Function
        }

        Because one of the parameters is type contained within namespace A, A::Function is being brought into scope, causing an error for the function Ambiguous. Is this part of the spec or is it some compiler (VS2012) jiggery pokery?

        A Offline
        A Offline
        Aescleal
        wrote on last edited by
        #3

        It's Argument Dependent Lookup (ADL) also known as Koenig lookup. It's invaluable when you're trying to define operations on a type in a namespace using functions in that namespace. Say you have:

        namespace N
        {
        class A
        {};

        friend A operator+( const A &, const A& );
        

        }

        // Somewhere else...

        A a, b;

        auto c = a + b;

        if the compiler didn't know to look for the operator in the namespace A was declared in you wouldn't be able to use operator functions in namespaces that easily. It would be a particular headache for <, << and >> which are required by chunks of the standard library.

        A 1 Reply Last reply
        0
        • A Aescleal

          It's Argument Dependent Lookup (ADL) also known as Koenig lookup. It's invaluable when you're trying to define operations on a type in a namespace using functions in that namespace. Say you have:

          namespace N
          {
          class A
          {};

          friend A operator+( const A &, const A& );
          

          }

          // Somewhere else...

          A a, b;

          auto c = a + b;

          if the compiler didn't know to look for the operator in the namespace A was declared in you wouldn't be able to use operator functions in namespaces that easily. It would be a particular headache for <, << and >> which are required by chunks of the standard library.

          A Offline
          A Offline
          Anthony Mushrow
          wrote on last edited by
          #4

          Super. I've probably got operator functions around that work because of this, I've just never noticed or thought about it before.

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups