Basic std:: question

HI, I’ve noticed in the dsp code there’s a quite a lot of std::sin and other trig functions, this is to make sure the correct overloaded function gets called as either a float or a double.
Do you know if it also uses these versions if I just put

using namespace std;

At the top of the file? Or is there some kind of other compiler preference going on?
I need the fastest one possible, and I find it messy using std:: everywhere, sorry about that. :smile:

JUCE already uses using namespace juce so if you used using namespace std in a JUCE project you’d have to write juce:: before every JUCE object!

I’m pretty sure you can use different namespaces at once?
I hear it’s ‘bad form’ to use it though…Because of accidentally picking up unknown alternatives.

Yes, you can use “using namespace” with different namespaces but as you said that is frowned upon because it increases the risk of name clashes.
Juce itself has added the possibility to not have an automatic “using namespace juce” exactly for that reason, and maybe if they were writing the library today they wouldn’t even add that “using namespace juce” for client code in first place.

That being said, be aware that in the body of your functions you can do what follows:

void function ()
    using std::sin;

    float v = sin (whatever);    // uses std::sin

That may save you some typing without polluting the global namespace (“using” directives are scope limited)


Excellent, thank-you. That seems safer.

It’s 5 characters though. What’s so hard about typing it out? (std::)

I know it seems trivial, but it started from me wondering which trig functions were being used when having templated types. I’m new to using templates, and it seemed odd seeing std:: everywhere, that’s all folks. :grinning: C++ is an odd language sometimes…