Show: Object Pascal C++
Display Preferences

Stricter C++ Compiler - Function Overload Resolution

From Appmethod Topics
Jump to: navigation, search

Go Up to Stricter C++ Compiler (Appmethod C++) Index

One of the areas where the Appmethod C++ 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 Embarcadero 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)'

std::abs Ambiguity

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;}

See Also

Personal tools
In other languages