Question

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))
Was it helpful?

Solution

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)
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top