fork(1) download
  1. #include <iostream>
  2. #include <type_traits>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. template <class T>
  7. class Container
  8. {
  9. public:
  10. template <class UR = T>
  11. void Add(UR&& object)
  12. {
  13. static_assert(std::is_same<std::remove_reference_t<UR>, T>::value, "UR and T should be the same!");
  14. m_Vector.push_back(std::forward<UR>(object));
  15. }
  16. void AddNonUR(T&& object)
  17. {
  18. m_Vector.push_back(std::forward<T>(object));
  19. }
  20. std::vector<T> m_Vector;
  21. };
  22.  
  23. class Data
  24. {
  25. public:
  26. int x;
  27. int y;
  28. };
  29.  
  30. int main() {
  31. Container<Data> list;
  32. Data d;
  33. list.Add(d);
  34. // list.AddNonUR(d); // Does not compile
  35. return 0;
  36. }
  37.  
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
Standard output is empty