1
0
mirror of https://github.com/alliedmodders/hl2sdk.git synced 2025-09-20 12:36:05 +08:00

Backport swap() -> V_swap() rename to avoid C++11 ADL ambiguity errors

The swap() function provided in the MathLib header was renamed to V_swap in recent Source SDK versions (e.g. 2013) to avoid causing ambiguity problems with std::swap(). But older SDK versions (such as TF2) lack this change, as they predate it.

The ambiguity between MathLib's swap() and std::swap() causes considerable problems when using newer features of C++ (such as std::unique_ptr) which internally call swap() in an unqualified manner to implement move semantics:

/usr/include/c++/5.2.0/bits/unique_ptr.h:342:6: error: call of overloaded ‘swap(MyType*&, MyType*&)’ is ambiguous
/usr/include/c++/5.2.0/bits/move.h:176:5: note: candidate: void std::swap(_Tp&, _Tp&) [with _Tp = MyType*]
hl2sdk-tf2/public/mathlib/mathlib.h:611:18: note: candidate: void swap(T&, T&) [with T = MyType*]

This patch backports the swap() -> V_swap() rename from the 2013 SDK version to the TF2 SDK version, so that the TF2 SDK can be used in conjunction with C++11 features such as std::unique_ptr without difficulty.

More information on why swap() isn't called in a namespace-qualified manner by standard library functions:
http://en.cppreference.com/w/cpp/language/adl#Notes
This commit is contained in:
sigsegv
2016-01-24 16:44:52 -08:00
parent 53c23fed59
commit 0edbd27fb8
26 changed files with 59 additions and 59 deletions

View File

@ -380,9 +380,9 @@ void MatrixInvert( const matrix3x4_t& in, matrix3x4_t& out )
Assert( s_bMathlibInitialized );
if ( &in == &out )
{
swap(out[0][1],out[1][0]);
swap(out[0][2],out[2][0]);
swap(out[1][2],out[2][1]);
::V_swap(out[0][1],out[1][0]);
::V_swap(out[0][2],out[2][0]);
::V_swap(out[1][2],out[2][1]);
}
else
{
@ -1264,18 +1264,18 @@ bool SolveInverseQuadraticMonotonic( float x1, float y1, float x2, float y2, flo
// first, sort parameters
if (x1>x2)
{
swap(x1,x2);
swap(y1,y2);
::V_swap(x1,x2);
::V_swap(y1,y2);
}
if (x2>x3)
{
swap(x2,x3);
swap(y2,y3);
::V_swap(x2,x3);
::V_swap(y2,y3);
}
if (x1>x2)
{
swap(x1,x2);
swap(y1,y2);
::V_swap(x1,x2);
::V_swap(y1,y2);
}
// this code is not fast. what it does is when the curve would be non-monotonic, slowly shifts
// the center point closer to the linear line between the endpoints. Should anyone need htis