RAD Studio
ContentsIndex
PreviousUpNext
Stricter C++ Compiler: Function Overload Resolution

One of the areas where the C++Builder 2007 compiler differs the most from the previous versions is in overload resolution, which includes the detection of ambiguity. The compiler now better conforms to the rules in section 13.3 of the 2003 C++ ANSI Standard. Several constructs that were previously allowed might now be reported as ambiguous or no match found, requiring that you modify code to clarify its intent.  

The compiler option -Vbo reverts to the old behavior, not enforcing the new stricter behavior. However, not all compiler changes can be controlled by this switch, so CodeGear recommends that you update the code instead.  

The following is an example of an ambiguity that was permitted by the previous compiler:

class X{};
  void foo(X);
  void foo(const X&);
  void ambig() {
    X x;
    foo(x); //error-ambiguous-the previous compiler chose 'void foo(x)'
  } 

The standard abs function might also generate an ambiguity message when invoked with a parameter that does not exactly match the types expected by the various overloaded versions of abs. Here is an example:

#include <limits>bool test(long l) {   return std::abs(l)> 0;} 

The code above generates an error and a warning:

Error E2015 test.cpp 5: Ambiguity between 'std::abs(int) at C:\dev\tp\sc\include\math.h:208' and 'std::abs(long double) at C:\dev\tp\sc\include\math.h:275' in function test(long)   
Warning W8057 test.cpp 6: Parameter 'l' is never used in function test(long)  

To fix this, cast to the type of the overload you want to invoke. For example:

  #include <limits>bool test(long l) {   return std::abs(static_cast<int>(l)) > 0;}
Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!