I am trying to write a class similar to std::set
, and I was wondering if I handled the perfect forwarding right in implementing the emplace
member function below. Basically, I forwarded calls to my class to a member set
. But I wasn't sure whether I used std::forward
enough.
Also, is there an easy way to tell when something is not forwarded as desired, and values are copied instead?
#include <set>
using namespace std;
template<typename T>
class myset {
std::set<T> m_s;
typedef typename std::set<T>::iterator iterator;
int cnt;
public:
template <class... Args>
pair <iterator,bool> emplace ( Args&&... args ) {
pair<iterator,bool> p = m_s.emplace(std::forward<Args>(args)...);
if (p.second) //true if a new element was inserted
cnt ++; //house keeping
return p;
}
};
int main() {myset<int> s;}
Also, is there an easy way to tell when something is not forwarded as desired, and values are copied instead?
sure, try passing a struct with deleted copy-constructor, defaulted move-constructor, defaulted move-operator=. If something doesn't work, you gonna get a compile-time error. \$\endgroup\$