C++ namespace,extending namespace

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<std::string> 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);
    //...
}    

延伸命名空間

根據namespace in C++ | Set 2 (Extending namespace and Unnamed namespace),我們可以定義多個同名的命名空間,第一個以後的命名空間只是對第一個命名空間的延伸。

如: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

參考連結

Namespace in C++ | Set 1 (Introduction)

namespace in C++ | Set 2 (Extending namespace and Unnamed namespace)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值