tags:

views:

261

answers:

1

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.cpp: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?

+4  A: 

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.

jk
ok, i had assumed 'using' set up a preference, not just a shortcut (as in, the last or first namespace would have priority), you say it doesn't which clarifies the issue nicely. Also yes, assimp uses math.h
SpliFF
Using import something from a scope (you can import a name from a class, which is especially useful when you try to overload a member function in a descendant) in another. If it results in ambiguity, you get errors at the point of use of the ambiguity, which is a sane design decision for a statically type language.
AProgrammer