std :: vector vs std :: initializer_list issue:undefined bahevior

时间:2016-05-09 15:54:08

标签: c++ c++11 vector initializer-list

我有这个辅助方法附加到链表(下面的代码):

void append(const std::initializer_list<T>& vals) {                               
            for (const auto& val : vals)                                                    
              append(val);                                                                  
} 

我首先尝试使用std :: vector附加1,5,7,9,如下所示:

void append(const std::vector<T>& vals) {                               
            for (const auto& val : vals)                                                    
              append(val);                                                                  
} 

但它用0,0,7,9代替1,5,7,9填充了链表。有什么想法吗?

以下是整个代码:

    template <typename T>                                                               
    class LinkedList {                                                                  
    private:                                                                            
      struct Node {                                                                     
        const T& val_;                                   
        std::shared_ptr<Node> next_ = nullptr;                                          
        Node(const T& val) : val_(val) {}                                               
      };                                                                                
    public:                                                                             
      std::shared_ptr<Node> head_ = nullptr;                                            
      std::shared_ptr<Node> current = nullptr;                                          

      // could be optimized                                                                                 
      void append(const std::initializer_list<T>& vals) {                               
        for (const auto& val : vals)                                                    
          append(val);                                                                  
      }                                                                                 

      void append(const T& val) {                                                       
        if (!head_) {                                                                   
          head_ = std::make_shared<Node>(val);                                          
          return;                                                                       
        }                                                                               
        current = head_;                                                                
        while (current->next_)                                                          
          current = current->next_;                                                     
        current->next_ = std::make_shared<Node>(val);                                   
      }                                                                                 

      template <typename Func>                                                          
      void for_each(Func f) {                                                           
        current = head_;                                                                
        while (current) {                                                               
          f(current->val_);                                                             
          current = current->next_;                                                     
        }                                                                               
      }                                                                                 


    };                                                                               

    int main() {                                                                     
      LinkedList<int> lst;                                                           
      lst.append({1,5,7,9});                                                         
      auto printer = [](int val) {                                                   
        std::cout << val << " ";                                                     
      };                                                                             
      lst.for_each(printer);                                                         
      std::cout << '\n';                                                                            
    }                                 

1 个答案:

答案 0 :(得分:4)

此处的一个编程错误是您的节点存储了引用const T& val_;。将值存储为const T val_;