using
using
與C語言裡的typedef
都是用來宣告別名的,那麼他們兩者有什麼差別呢?根據What is the difference between ‘typedef’ and ‘using’ in C++11?:
They(typedef and using) are largely the same, except that:
The alias declaration(using) is compatible with templates, whereas the C style typedef is not.
以下兩種寫法等價:
typedef int MyInt;
using MyInt = int;
至於在使用了template
的情況下,則一定要用using
,以下代碼來自TensorRT/samples/common/buffers.h
:
using DeviceBuffer = GenericBuffer<DeviceAllocator, DeviceFree>;
using HostBuffer = GenericBuffer<HostAllocator, HostFree>;
參考連結
What is the difference between ‘typedef’ and ‘using’ in C++11?