RAD Studio |  |

Stricter C++ Compiler: Function Overload Resolution
std::abs Ambiguity | See Also
Collapse All

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:

Copy Code

    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:

Copy Code

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

The code above generates an error and a warning:

Copy Code

    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:

Copy Code

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

See Also

Stricter C++ Compiler (C++Builder 2007)

Binding of References and Qualifiers

String Literals Are Now Constants

Template Changes

Initialization and Conversion

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!