samedi 26 mai 2018

How to declare the return of `emplace()` of an instantiated `std::set`?

  1. I have a class template:

    template<class idxT>
    class SimpleGraph {
    private:
      // ...
      std::set<std::pair<idxT,idxT>> edgeSet;
      // ...
      void addEdge(idxT a, idxT b);
      // ...
    }
    
    
  2. I need to use the return of edgeSet.emplace(a,b) inside addEdge(), which is denoted as r in the following.
  3. The declaration and the assignment of r need to be separated since the value of a and b is determined by a if-else statement. Thus, I cannot use

    auto r = edgeSet.emplace(a,b);
    
    
  4. I tried the following strategies but neither is successful. The first one is

    std::pair<decltype(edgeSet)::iterator,bool> r;
    // some code
    r = edgeSet.emplace(a,b);
    
    

    The second one is

    // inside class template
    typedef std::set<std::pair<idxT,idxT>> EdgeSetType;
    EdgeSetType edgeSet;
    
    // inside definition of `addEdge()`
    std::pair<EdgeSetType::iterator,bool> r;
    // some code
    r = edgeSet.emplace(a,b);
    
    

How can I declare the return of edgeSet.emplace(a,b)?

Aucun commentaire:

Enregistrer un commentaire