C++ typedef struct v.s. struct

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++中是怎麼回事呢?摘自Difference between ‘struct’ and ‘typedef struct’ in C++ program?

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[])
{
    //...
}

參考連結

Difference between ‘struct’ and ‘typedef struct’ in C++?

Difference between ‘struct’ and ‘typedef struct’ in C++ program?

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

猜你喜欢

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