fork(1) download
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. struct string_view
  6. {
  7. string_view() = default;
  8. string_view(const string_view& other) = default;
  9.  
  10. string_view(const string& str)
  11. : ptr(str.c_str()), size(str.length())
  12. {}
  13.  
  14. string_view(const char* str)
  15. : ptr(str), size(::strlen(str))
  16. {}
  17.  
  18. string_view(const char* str, size_t len)
  19. : ptr(str), size(len)
  20. {}
  21.  
  22. // Protection for temporary objects
  23. string_view(string&& str) = delete;
  24.  
  25. string_view& operator=(const string_view&) = default;
  26.  
  27. const char *ptr = nullptr;
  28. size_t size = 0;
  29. };
  30.  
  31. string get_host()
  32. {
  33. return "http://h...content-available-to-author-only...d.su";
  34. }
  35.  
  36. string_view get_schema(string_view uri)
  37. {
  38. return uri;
  39. }
  40.  
  41. string_view get_schema2(string url)
  42. {
  43. string_view v(url);
  44. return v;
  45. }
  46.  
  47. const char* get_schema3(string url)
  48. {
  49. return url.c_str();
  50. }
  51.  
  52. int main()
  53. {
  54. {
  55. // Compilation error:
  56. //auto v1 = get_schema(get_host());
  57.  
  58. //string_view v2;
  59. //v2 = get_host();
  60. }
  61.  
  62. {
  63. // WA for protection
  64. auto v1 = get_schema2(get_host());
  65. auto v2 = get_schema(get_host().c_str());
  66. }
  67.  
  68. {
  69. // Valid usage
  70. string str = get_host();
  71. auto v1 = get_schema(str);
  72.  
  73. string_view v2 = "hello";
  74.  
  75. auto v3 = get_schema("http://h...content-available-to-author-only...d.su");
  76. }
  77.  
  78.  
  79.  
  80. return 0;
  81. }
  82.  
  83.  
Success #stdin #stdout 0s 3408KB
stdin
Standard input is empty
stdout
Standard output is empty