Clang VS VC++:“error: declaration of 'T' shadows template parameter”

后端 未结 1 749
情话喂你
情话喂你 2020-12-31 16:31

I was trying to compile the following code for the classic copy&swap idiom on my Mac with clang 3.3

template class node{

private:
             


        
相关标签:
1条回答
  • 2020-12-31 17:12

    I believe that this is what you want (which happens to be what you just added to your question as the third option)

    #include <utility>
    
    template <typename T> class node;
    template <typename T> void swap(node<T> & a, node<T> & b);
    
    template<typename T> class node {
        private:
            node<T>* left;
            node<T>* right;
            T value;
    
        public:
            friend void swap<>(node<T>&, node<T>&);
    };
    
    template <typename T> void swap(node<T> & a, node<T> & b) {
        std::swap(a.left, b.left);
        std::swap(a.right, b.right);
        std::swap(a.value, b.value);
    }
    
    int main() {
        node<int> x, y;
        swap(x, y);
    }
    
    0 讨论(0)
提交回复
热议问题