using directive vs using declaration swap in C++

前端 未结 3 1482
囚心锁ツ
囚心锁ツ 2020-12-24 08:08

Please refer to the code below:

#include 

namespace N
{

    template 
    class C
    {
    public:
        void SwapWit         


        
相关标签:
3条回答
  • 2020-12-24 08:44

    Note: I have removed your swap definition in namespace std. It is not relevant here. Even with out it the code will have the same issues.


    This is due to look up rule differences between using directive(using namespace std) and the using declaration(using std::swap)

    Microsoft says

    If a local variable has the same name as a namespace variable, the namespace variable is hidden. It is an error to have a namespace variable with the same name as a global variable.

    #include<iostream>
    
    namespace T {
        void flunk(int) { std::cout << "T";}
    }
    
    namespace V {
        void flunk(int) { std::cout << "V";}
    }
    
    
    int main() {
        using T::flunk;   // makes T::flunk local
        // using V::flunk;  // makes V::flunk local. This will be an error
        using namespace V;  // V::flunk will be hidden
        flunk(1);
    }
    

    According to this, due to your

    template <typename T>
    void swap(C<T> & c1, C<T> & c2)
    

    std::swap will be hidden when you use

    using namespace std; 
    

    So the only swap available for template deduction is N::swap and it won't work for ints because it expects a template class as argument.

    but not when

    using std::swap;
    

    In this case it becomes equivalent to local definition. And can be used with out problem.

    0 讨论(0)
  • 2020-12-24 08:54

    The first case is a using directive (using namespace X), and what it means is that the names from namespace X will be available for regular lookup, in the first common namespace of X and the current scope. In this case, the first common namespace ancestor of ::N and ::std is ::, so the using directive will make std::swap available only if lookup hits ::.

    The problem here is that when lookup starts it will look inside the function, then inside the class, then inside N and it will find ::N::swap there. Since a potential overload is detected, regular lookup does not continue to the outer namespace ::. Because ::N::swap is a function the compiler will do ADL (Argument dependent lookup), but the set of associated namespaces for fundamental types is empty, so that won't bring any other overload. At this point lookup completes, and overload resolution starts. It will try to match the current (single) overload with the call and it will fail to find a way of converting from int to the argument ::N::C and you get the error.

    On the other hand a using declaration (using std::swap) provides the declaration of the entity in the current context (in this case inside the function itself). Lookup will find std::swap immediately and stop regular lookup with ::std::swap and will use it.

    0 讨论(0)
  • 2020-12-24 09:04

    The obvious reason is that a using declaration and a using directive have different effects. A using declaration introduces the name immediately into the current scope, so using std::swap introduces the name into the local scope; lookup stops here, and the only symbol you find is std::swap. Also, this takes place when the template is defined, so later declarations in namespace std aren't found. In the following line, the only swap that will be considered is the one defined in <algorithm>, plus those added by ADL (thus, the one in namespace N). (But is this true with VC++? The compiler doesn't implement name lookup correctly, so who knows.)

    A using directive specifies that the names will appear "as if" they were declared in the nearest namespace enclosing both the directive and the nominated namespace; in your case, global namespace. And it doesn't actually introduce the names; it simply affects name lookup. Which in the case of a dependent symbol (or always, in the case of VC++) takes place at the call site.

    As for why you have this particular error message: probably more an issue with VC++, since there's certainly no non-deduceable contexts in your code. But there's no reason to expect the two variants be have the same behavior, regardless of the compiler.

    0 讨论(0)
提交回复
热议问题