0

I'm using a lot of the std::chrono library and my code is becoming quite verbose with things like std::chrono::x::y etc.

I do not want to use using namespace std; because this potentially shadows a lot of things in my program.

That said if I have using namespace std::chrono, now I don't have to type chrono at all. I'd prefer to specify things at that level e.g. then I can fully qualify with e.g. chrono::microseconds. I don't want to simply specify microseconds.

My class where I'd like to use this looks something like this

namespace MyNamespace {
class MyClass {
// lots of std::chrono stuff
};
}

I've tried:

  • using namespace chrono = std::chrono (inside the class)

Results in

error: expected nested-name-specifier before ‘namespace’
 using namespace chrono = std::chrono;

^ This one is actually perplexing to me, searching for it usually yields results for those with syntax errors but the program compiles fine without this line.

  • using chrono = std::chrono; (same result whether inside class or outside class)

Results in:

error: ‘chrono’ in namespace ‘std’ does not name a type
 using chrono = std::chrono;
  • using namespace chrono = std::chrono (outside class, within MyNameSpace)

Results in:

error: ‘chrono’ is not a namespace-name
 using namespace chrono = std::chrono;
                 ^~~~~~
brenzo
  • 537
  • 1
  • 4
  • 11
  • 1
    it's just `namespace chono = std::chono;`, see https://en.cppreference.com/w/cpp/language/namespace_alias and afaik you can only do so at namespace scope; you could of course add `using` for specific types... – fabian Dec 29 '21 at 19:08

0 Answers0