How can you safely use a namespace for a class or title?

I would like to be able to use references to another namespace inside the class, without making a mistake / sin, in order to introduce this namespace into my calling compilation units.

The only way I know now is to move everything to my .cpp file, where I can declare using namespace XYZwithout worries.

But what if I define a template? Or do I just want to put this code in my header?

For example, I want to use the Toolbox namespace from which it Toolbox::FormatStringlives, and there are many other useful tools, but I can’t? Should I indicate each link how Toolbox::FormatString?

class CHaspException : public CLabeledException
{
public:
    CHaspException(haspStatus status) :
        CLabeledException(FormatString(_T("HASP Error %u: %s"), (unsigned)status, GetHaspErrorMessage(status))),
        m_status(status)
    {
    }

    CHaspException(const char * source, haspStatus status) :
        CLabeledException(FormatString(_T("%hs: HASP Error %u: %s"), source, (unsigned)status, GetHaspErrorMessage(status))),
        m_status(status)
    {
    }

    CHaspException(const char * source, const char * api, haspStatus status) :
        CLabeledException(FormatString(_T("%hs: %hs() returned %u: %s"), source, api, (unsigned)status, GetHaspErrorMessage(status))),
        m_status(status)
    {
    }

    CHaspException(const char * source, const char * api, const CString & args, haspStatus status) :
        CLabeledException(FormatString(_T("%hs: %hs(%s) returned %u: %s"), source, api, args, (unsigned)status, GetHaspErrorMessage(status))),
        m_status(status)
    {
    }

    haspStatus GetStatus() const { return m_status; }

private:
    haspStatus  m_status;
};

, , . , begin() end(), std , - :

template <typename T> myfun(T & x)
{
  using namespace std;
  if (begin(x) == end(x))...
}

, std begin/end, ( std::being(x)).

, -, , , no?

?


- . " , , ", .

, " ", , . ...

+4

Source: https://habr.com/ru/post/1694693/


All Articles