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?