Scenario
I have a class template:
template<class idxT> class SimpleGraph { private: // ... std::set<std::pair<idxT,idxT>> edgeSet; // ... void addEdge(idxT a, idxT b); // ... }
- I need to use the return of
edgeSet.emplace(a,b)
insideaddEdge()
, which is denoted asr
in the following. The declaration and the assignment of
r
need to be separated since the value ofa
andb
is determined by a if-else statement. Thus, I cannot useauto r = edgeSet.emplace(a,b);
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);
Question
How can I declare the return of edgeSet.emplace(a,b)
?
Update 1: a verifiable example for the first trial
#include <set> // set
#include <utility> // pair
#include <iostream>
template<class idxT>
class SimpleGraph {
public:
void test(void);
private:
std::set<std::pair<idxT,idxT>> edgeSet;
void addEdge(idxT a, idxT b);
};
template<class idxT>
void SimpleGraph<idxT>::test(void) {
addEdge(1,2);
addEdge(1,2);
}
template<class idxT>
void SimpleGraph<idxT>::addEdge(idxT a, idxT b) {
std::pair<decltype(edgeSet)::iterator,bool> r;
if (a < b) {
r = edgeSet.emplace(a,b);
}
else {
r = edgeSet.emplace(a,b);
}
if (!r.second) {
std::cerr << "Error on (" << a << ',' << b <<"): "
<< "mutiple edges are not allowed for a simple graph.\n";
exit(1);
}
}
int main(void) {
SimpleGraph<int> graph;
graph.test();
}
clang complains that
test.cpp:24:13: error: template argument for template type parameter must be a type; did you forget 'typename'?
std::pair<decltype(edgeSet)::iterator,bool> r;
See also here for the compile-time error generated by gcc.