fork download
  1. #include <iostream>
  2. #include <set>
  3. #include <sstream>
  4. #include <string>
  5.  
  6. // trying to resemble the OP
  7.  
  8. template <typename VALUE>
  9. class MultiAccessorT {
  10. public:
  11. typedef VALUE Value;
  12. typedef std::set<Value> Table;
  13. private:
  14. Table &_tbl;
  15. public:
  16. MultiAccessorT(Table &tbl): _tbl(tbl) { }
  17.  
  18. typename Table::iterator operator[](const std::string &str)
  19. {
  20. std::cout << "using operator[](const std::string&) ";
  21. std::istringstream cvt(str);
  22. Value value;
  23. return cvt >> value ? _tbl.find(value) : _tbl.end();
  24. }
  25.  
  26. typename Table::iterator operator[](long value)
  27. {
  28. std::cout << "using operator[](long) ";
  29. return _tbl.find((Value)value);
  30. }
  31.  
  32. typename Table::iterator operator[](double value)
  33. {
  34. std::cout << "using operator[](double) ";
  35. return _tbl.find((Value)value);
  36. }
  37.  
  38. typename Table::iterator operator[](char value)
  39. {
  40. std::cout << "using operator[](char) ";
  41. return _tbl.find((Value)value);
  42. }
  43.  
  44. // This resembles my personal "hair-killer":
  45. typename Table::iterator operator[](bool value)
  46. {
  47. std::cout << "using operator[](bool) ";
  48. return value ? _tbl.begin() : _tbl.end();
  49. }
  50. };
  51.  
  52. // checking out
  53.  
  54. int main()
  55. {
  56. // build some sample data
  57. std::set<unsigned> tbl = { 0, 2, 4, 6, 8, 10, 12, 14 };
  58. // template instance and instance
  59. MultiAccessorT<unsigned> acc(tbl);
  60. // test the operators
  61. std::cout << "MultiAccessorT::operator[](const string&): ";
  62. std::cout << (acc[std::string("6")] != tbl.end() ? "found." : "missed!") << std::endl;
  63. std::cout << "MultiAccessorT::operator[](long): ";
  64. std::cout << (acc[6L] != tbl.end() ? "found." : "missed!") << std::endl;
  65. std::cout << "MultiAccessorT::operator[](double): ";
  66. std::cout << (acc[6.0] != tbl.end() ? "found." : "missed!") << std::endl;
  67. std::cout << "MultiAccessorT::operator[](char): ";
  68. std::cout << (acc['\6'] != tbl.end() ? "found." : "missed!") << std::endl;
  69. // examples which force conversion
  70. std::cout << "using a constant string \"6\" (type const char[2]) ";
  71. std::cout << (acc["6"] != tbl.end() ? "found." : "missed!") << std::endl;
  72. /* FAILS:
  73.   std::cout << "using a int constant 6";
  74.   std::cout << (acc[6] != tbl.end() ? "found." : "missed!") << std::endl;
  75.   g++ error: ambiguous overload for 'operator[]' (operand types are 'MultiAccessorT<unsigned int>' and 'int')
  76.   Found candidates are:
  77.   typename Table::iterator operator[](long value)
  78.   typename Table::iterator operator[](double value)
  79.   typename Table::iterator operator[](char value)
  80.   */
  81. // done
  82. return 0;
  83. }
Success #stdin #stdout 0s 4348KB
stdin
Standard input is empty
stdout
MultiAccessorT::operator[](const string&): using operator[](const std::string&) found.
MultiAccessorT::operator[](long): using operator[](long) found.
MultiAccessorT::operator[](double): using operator[](double) found.
MultiAccessorT::operator[](char): using operator[](char) found.
using a constant string "6" (type const char[2]) using operator[](bool) found.