Odd VS name mangling behavior?

拜拜、爱过 提交于 2019-12-01 22:32:43

Yes, this is a bug in the Visual C++ name decoration scheme. For pointer-type parameters, top-level const and volatile qualifiers are encoded into the decorated name, even though they are not relevant to the type of the function. So, for example, char** and char** const are encoded differently. (In your example, char*[] is equivalent to char** const.)

When determining how to decorate the function name, the compiler will use the first declaration of the function, even if the definition does not exactly match the first declaration. This is why your example links when the definition is in the same source file as the main function: The test function is decorated with the name required by the first declaration, which is the same name that is referred to from within the main function.

If you move both the declaration and the definition into a separate source file, e.g.,

int test(int argc, char *argv[]);

int test(int argc, char **argv)
{
   return 0;
}

then your program will also link successfully, for the same reason. This is why this "bug" is not usually a problem: Usually when functions are used across multiple translation units, they are declared in a header file and there is one declaration that is included everywhere.

For the separate file, use test(int argc, char ** const argv), based on the error message. Note that the address of an array (char * argv[]) would be constant (so argv would be constant), as opposed to a pointer to pointer (char **argv). Although since argv is passed by value, it can't be modified so I'm not sure why VS is being picky about this.

When both functions are in the same file, apparently VS can detect that test() does not modify argv, so it doesn't complain.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!