You can use a typedef to create a shorter and simpler name for types:
typedef std::chrono::high_resolution_clock Clock;
typedef Clock::time_point TimePoint;
typedef std::chrono::seconds Seconds;
typedef std::chrono::milliseconds Milliseconds;
As well as for instantiated templated types:
typedef std::chrono::duration<float, std::ratio<1>> RealDuration;
// Example usage
float dt = RealDuration(a - b).count();
And for function pointers:
typedef void (*FuncPtr)(int,int);
You can also use type aliases for templates:
template<typename T> using uptr = std::unique_ptr<T>;
// Example usage
uptr<int> myInt;
uptr<foo> myFoo;
But how can you create an alias/pointer to a templated function? For example, if I want to be able to use the name DurationCast to write things like this:
x = DurationCast<Seconds>(a - b);
y = DurationCast<Milliseconds>(c - d);
What needs to be done to shorten the function std::chrono::duration_cast<T>()
to just DurationCast<T>()
without simply going the using namespace std::chrono;
or using std::chrono::duration_cast;
route, and without writing my own function objects to achieve it?
EDIT: I guess I can just write a simple wrapper around it:
template<typename ToType, typename FromType>
ToType DurationCast(const FromType& d)
{
return std::chrono::duration_cast<ToType>(d);
}
Doesn't work like an alias, but the end result is that I can use it in the exact same way I was aiming for:
x = DurationCast<Seconds>(a - b);