0

The following code cannot be compiled by VC++ and clang.

int f()
{
    return 0;
}

int main()
{
    // error : called object type 'int' is not a function or function pointer
    int f = f(); 
}

It is necessary in some cases. For example, I have a function to calculate the character count of a string, which is named count, however, another function parameter is also expressively named as count.

size_t count(char* sz)
{
    return strlen(sz);
}

bool check_count(char* sz, size_t count)
{
    return count == count(sz); // ???
}

How to resolve this issue?

xmllmx
  • 39,765
  • 26
  • 162
  • 323

2 Answers2

1

In C++ you can define a namespace for your objects, in your example you could do:

namespace MyFunctions {


    int f()
    {
        return 0;
    }
}

int main()
{
    int f = MyFunctions::f(); 
} 
AngeloDM
  • 397
  • 1
  • 8
1

The answer is simple. This is not supported. C, as many other languages, cannot support absolutely every scenario. It is unreasonable to put out such a goal. Nobody ever tried to achieve this.

In your particular case, you should rename your parameter. Function always has limited scope. It is always recompiled as a whole. Names of params in prototypes in the header files may have different names. Renaming param in the body of the function will work in 99.9999% of cases.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51