An Example Of Namespace Clashes

Thanks to @DanM for this simple example of code that could cause an ambiguous function call…

namespace Triangle
{
    int area(int h, int w)
    {
        return h * w / 2;
    }
}
int area(int h, int w)
{ 
    return h * w;
}

using namespace Triangle;
int main()
{
    int a = area(4,2) //ambiguous call Triangle::area or ::area?
}

This could be even worse if the Triangle namespace were included in a header file we aren’t aware of.

10 Likes

One more fact for using

  • xyz::command

instead of

  • using namespace xyz;

Privacy & Terms