C++ namespace,extending namespace
发布日期:2021-05-06 19:47:41 浏览次数:27 分类:精选文章

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

C++ namespace,extending namespace

namespace

一個變數或函數默認是全局可見的(global scope);如果我們在一個命名空間裡定義函數或變數,那麼它們便只於該命名空間中可見(namespace scope),也因此在不同命名空間裡定義相同名字的變數是合法的。

來自TensorRT/samples/common/argsParser.h,關於命名空間samplesCommon的定義:

namespace samplesCommon{   //...struct CaffeSampleParams : public SampleParams{       std::string prototxtFileName; //!< Filename of prototxt design file of a network    std::string weightsFileName;  //!< Filename of trained weights file of a network    std::string meanFileName;     //!< Filename of mean file of a network};//...struct Args{       bool runInInt8{   false};    bool runInFp16{   false};    bool help{   false};    int useDLACore{   -1};    int batch{   1};    std::vector
dataDirs; bool useILoop{ false};};//...} // namespace samplesCommon

如果我們想在global scope裡存取某命名空間裡的變數或函數,則需要在其前面加上該命名空間的名字,如在TensorRT/samples/opensource/sampleMNIST/sampleMNIST.cpp 中:

int main(int argc, char** argv){       samplesCommon::Args args;    //...    samplesCommon::CaffeSampleParams params = initializeSampleParams(args);    //...}

延伸命名空間

根據,我們可以定義多個同名的命名空間,第一個以後的命名空間只是對第一個命名空間的延伸。

如:TensorRT/include/NvCaffeParser.h裡首先定義了nvcaffeparser1命名空間:

namespace nvcaffeparser1{   class IBlobNameToTensor{   //...};class IBinaryProtoBlob{   //...};class IPluginFactory{   //...};class IPluginFactoryExt : public IPluginFactory{   //...};class IPluginFactoryV2{   //...};class ICaffeParser{   //...};TENSORRTAPI ICaffeParser* createCaffeParser() TRTNOEXCEPT;TENSORRTAPI void shutdownProtobufLibrary() TRTNOEXCEPT;} // namespace nvcaffeparser1

TensorRT/parsers/caffe/blobNameToTensor.h則繼續對此命名空間做延伸:

namespace nvcaffeparser1{   class BlobNameToTensor : public IBlobNameToTensor{   //...};} // namespace nvcaffeparser1

參考連結

上一篇:C++ struct的繼承
下一篇:TensorRT/samples/common/logger.h,logger.cpp,logging.h源碼研讀

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年03月13日 08时38分45秒