:: scope resolution operator in front of a template function call in c++

亡梦爱人 提交于 2021-01-27 04:34:55

问题


I'm stuck with templates and scope resolution operator. I found these line in a file, I'm not able to figure out why we are using :: in front of a template function call, as of my knowledge we can only use :: in front of variables when refering to a global variable. Any Idea will be helpful

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

回答1:


The scope resolution operator :: (at the beginning) forces the compiler to find the identifier from the global scope, without it the identifier is found relative to the current scope.

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)
}


来源:https://stackoverflow.com/questions/8937824/scope-resolution-operator-in-front-of-a-template-function-call-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!