Reputation: 237
In Visual Studio 2008, the following code will build successfully:
void Foo (int x);//Prototype
void Foo(const int x)//Implementation
{
}
However, this will generate an error (unresolved external symbol) during linking:
void Foo (int x);//Prototype
void Foo(const int x)//Implementation
{
}
void Bar()
{
int x = 0;
Foo(x);
}
The problem is because the function implementation for Foo specifies that the integer argument is constant, while the prototype does not require a constant.
Why does this happen? How come the problem isn't detected during compilation?
Upvotes: 0
Views: 154
Reputation: 507413
It's because you have a broken compiler. Broken compilers can choose their behavior themselfs. I know some compilers have this broken handling of that case.
Look on MSDN about a manual that documents that broken behavior. It works fine with compliant (non-broken in this regard) compilers.
Upvotes: 2
Reputation: 263360
Just to make absolutely sure that Armen is not crazy:
#include <type_traits>
int main()
{
static_assert( std::is_same<void(int ), void(const int )>::value, "...");
static_assert(!std::is_same<void(int&), void(const int&)>::value, "...");
}
This should compile fine on any standard-conforming compiler with minimum C++0x support. If it does not, the compiler is broken. For example, g++ 4.5.1 accepts this code just fine.
And by the way, your actual problem is that you define a Foo
but call a foo
(note the different case).
Upvotes: 0
Reputation: 133122
The problem is that when you call the function you call foo
, but you have defined the Foo
(with capital F). Both Foo's (re)declare the same function, because top-level consts on parameters do not contribute to the function's signature. they are ignored if the declaration is not a definition and if it is a definition then the parameter variable is considered const inside the function. Think about it - if the parameter is passed as a copy (by value), then it is absolutely irrelevant for the caller whether the function will change the copy or not. That's why function declarations that differ only in top-level consts on their parameters are considered to declare the same function.
Upvotes: 2
Reputation: 14327
Because you compile in C++, so the compiler considers the two functions (one with const param and one with non-const param) as two separate functions (with different decorated name). Only one of them is implemented, and the other one is used, hence the linking error.
Upvotes: -3