C++ typedef struct v.s. struct
发布日期:2021-05-06 19:47:44 浏览次数:26 分类:原创文章

本文共 927 字,大约阅读时间需要 3 分钟。



C++ typedef struct v.s. struct








typedef struct v.s. struct


在C語言中,定義完一個struct之後,我們通常還會加上typedef。這樣以後用到struct xxx時,我們就能用yyy代指,而不用老是加上struct這個關鍵字:


struct xxx {
...};typedef struct xxx yyy;

而在C++中是怎麼回事呢?摘自:


In C++, there is no difference between 'struct' and 'typedef struct' because, in C++, all struct/union/enum/class declarations act like they are implicitly typedef'ed, as long as the name is not hidden by another declaration with the same name.

在C++中,宣告struct時就隱性地為它typedef過了。所以我們不需要使用typedef xxx yyy,就能直接不帶struct關鍵字地使用該struct


TensorRT/samples/common/argsParser.h中,定義完Args這個結構體後,不需要先typedef,就能直接在parseArgs函數裡不帶struct關鍵字地使用它:


struct Args{
bool runInInt8{
false}; bool runInFp16{
false}; bool help{
false}; int useDLACore{
-1}; int batch{
1}; std::vector<std::string> dataDirs; bool useILoop{
false};};inline bool parseArgs(Args& args, int argc, char* argv[]){
//...}

參考連結



上一篇:TensorRT/samples/common/argsParser.h源碼研讀
下一篇:C++ struct初始化

发表评论

最新留言

很好
[***.229.124.182]2025年04月07日 01时07分33秒