fork download
  1. #include <string>
  2. #include <iostream>
  3.  
  4. extern const char SQL_SELECT[] = "SELECT ";
  5. extern const char SQL_FROM[] = "FROM ";
  6. extern const char SQL_WHERE[] = "WHERE ";
  7. extern const char SQL_ORDER_BY[] = "ORDER_BY ";
  8.  
  9. class Fields
  10. {
  11. public:
  12. Fields &operator ,(const std::string &s)
  13. {
  14. SQL.append(s).append(1, ',');
  15. return *this;
  16. }
  17.  
  18. Fields &operator ,(const Fields &f)
  19. {
  20. std::string Result = f;
  21. SQL.append(Result);
  22. return *this;
  23. }
  24.  
  25. virtual operator std::string() const = 0;
  26. protected:
  27. std::string SQL;
  28. };
  29.  
  30. template <const char *INSTRUCTION> struct Instruction : public Fields
  31. {
  32. operator std::string() const
  33. {
  34. std::string Result(INSTRUCTION);
  35. return Result.append(SQL);
  36. }
  37. };
  38.  
  39. enum ORDER
  40. {
  41. ASC,
  42. DESC,
  43. };
  44.  
  45. template <> struct Instruction<SQL_ORDER_BY> : public Fields
  46. {
  47. using Fields::operator std::string;
  48. using Fields::operator,;
  49.  
  50. Instruction() : order(ASC) {}
  51.  
  52. Fields &operator ,(const ORDER o)
  53. {
  54. order = o;
  55. return *this;
  56. }
  57.  
  58. operator std::string() const
  59. {
  60. std::string Result(SQL_ORDER_BY);
  61. Result.append(SQL);
  62. Result.append(order == ASC? "ASC": "DESC");
  63. return Result;
  64. }
  65.  
  66. private:
  67. ORDER order;
  68. };
  69.  
  70. typedef Instruction<SQL_SELECT> SELECT;
  71. typedef Instruction<SQL_FROM> FROM;
  72. typedef Instruction<SQL_WHERE> WHERE;
  73. typedef Instruction<SQL_ORDER_BY> ORDER_BY;
  74.  
  75. int main(int argc, char **argv)
  76. {
  77. std::string Query = ((SELECT(), "a", "b", "c"),
  78. (FROM(), "A", "B"),
  79. (WHERE(), "a = b AND c <> b"),
  80. (ORDER_BY(), "a", "c", DESC));
  81.  
  82. std::cout << Query << '\n';
  83.  
  84. return 0;
  85. }
  86.  
Compilation error #stdin compilation error #stdout 0s 3020KB
stdin
Standard input is empty
compilation info
prog.cpp: In function 'int main(int, char**)':
prog.cpp:80:54: error: conversion from 'ORDER' to non-scalar type 'std::string' requested
stdout
Standard output is empty