7

我坚持使用模板和范围解析运算符。我在一个文件中找到了这些行,我无法弄清楚为什么我们在模板函数调用前面使用 ::,据我所知,当引用全局变量时,我们只能在变量前面使用 :: . 任何想法都会有所帮助

#define CREATE_AND_DECODE_TYPE(Type, buffer, pType) \
    ::CreateAndDecodeType<Type>(buffer, pType, throwVarBindExceptions, static_cast<Type *>(NULL))
4

1 回答 1

14

范围解析运算符 :: (在开头)强制编译器从全局范围中查找标识符,没有它,标识符是相对于当前范围找到的。

namespace X
{
    namespace std
    {
        template<typename T>
        class vector {};
    }

    std::vector<int>     x;       // This is X::std::vector
    ::std::vector<int>   y;       // This is the std::vector you normally expect (from the STL)
}
于 2012-01-20T07:41:01.033 回答