C++ using

C++ using

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?

发布了74 篇原创文章 · 获赞 9 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/keineahnung2345/article/details/104072444