开发者

Resolving ambiguous calls in C++ with namespaces

I'm merging a static library (assimp) into an existing project (Spring RTS) where both the library and the project are under regular development. I'm trying to add the library in such a way that I can easily repeat the integration as new releases come out.

Anyway, the issue is that Spring requires the library to perform all maths using the streflop maths library. In practice this means min(x,y) should be replaced with streflop::min(x,y) everywhere it is used (which is a lot, considering the issue applies to all maths functions).

I could do a mass regex replace but I was hoping for something a little more elegant. After some research and testing it seemed that adding using namespace streflop; at the top of each .cpp file would do the trick but it didn't.

The exact error is:

/mnt/work/workspace/spring-patch-git/spring/rts/lib/assimp/code/FixNormalsStep.cp开发者_JS百科p:147: error: call of overloaded sqrtf(const float&) is ambiguous
/usr/include/bits/mathcalls.h:157: note: candidates are: float sqrtf(float)
/mnt/work/workspace/spring-patch-git/spring/rts/lib/streflop/SMath.h:347: note:                 streflop::Simple streflop::sqrtf(streflop::Simple)

I thought the whole point of namespaces was to resolve this kind of thing but it doesn't seem to work here. I'm a bit confused by the reference to streflop::Simple as well. Is this a nested namespace and could that be part of the reason it isn't working as expected?


If you only need the min function from the streflop namespace, you can use

using streflop::min;

instead of

using namespace streflop;

This will import only the name min, not the whole namespace.

Your error is because what you are doing imports every name from the streflop namespace so that they can be used unqualified, and sqrtf already exists unqualified. Are you perhaps including C header files as they are in C? That is, using math.h instead of cmath? Because if you use the C++ headers like cmath, the functions from the standard library will be in the std namespace and you shouldn't get a clash even if you import the whole streflop namespace.

Another option is that if the places where you now get errors from are few, you can explicitly qualify them. Like in this case, you can replace sqrtf with either streflop::sqrtf or ::sqrtf, depending on which version you want to use.

The streflop::Simple has little to do with your issue; it is just the parameter type and return value for streflop::sqrtf. The only way it is involved is that in overload resolution it gets treated like float so that both of the sqrtf functions listed are possible to call and the compiler cannot determine which one you meant.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜