I am trying to use alias for std::chrono
and std::chrono::duration_cast<std::chrono::seconds>
which are throwing compilation errors. I included <chrono>
header file. Below is the code:
#include <chrono>
class abc {
public:
using co = std::chrono; // Throwing compilation error
using coDurationCast = std::chrono::duration_cast<std::chrono::seconds>; // Throwing compilation error
using timeType = std::chrono::time_point<std::chrono::system_clock>; // Surprisingly this is working fine
};
Error:
error: ‘chrono’ in namespace ‘std’ does not name a type
error: ‘std::chrono::duration_cast’ in namespace ‘std::chrono’ does not name a template type
I am unable to understand why it is throwing this error. Surprisingly, the alias is working for std::chrono::time_point<<std::chrono::system_clock>>
.
Can anyone please help me to understand why it is throwing this error?
namespace co = std::chrono;
Not allowed at class scope though.std::chrono::duration_cast
is a function template; you can't create an alias of a function.std::chrono::time_point
is a class template; you can create an alias for a type. In other words: if you haveclass C{}; void f();
you can writeusing C_alias = C;
but notusing f_alias = f;
std::chrono::time_point<std::chrono::system_clock>
is a type, and you can create aliases to types.std::chrono::duration_cast<std::chrono::seconds>
is a function. and you cant create aliases to functions.using std::chrono::duration_cast;