当前位置:   article > 正文

蓝桥ROS机器人之现代C++学习笔记2.2变量及其初始化_ros中c_cpp中的include变量会变

ros中c_cpp中的include变量会变

if/switch 变量声明强化

  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. int main() {
  5. std::vector<int> vec = {1, 2, 3, 4};
  6. // after c++17, can be simplefied by using `auto`
  7. const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 2);
  8. if (itr != vec.end()) {
  9. *itr = 3;
  10. }
  11. const std::vector<int>::iterator itr1 = std::find(vec.begin(), vec.end(), 3);
  12. if (itr1 != vec.end()) {
  13. *itr1 = 4;
  14. }
  15. // should output: 1, 4, 3, 4. can be simplefied using `auto`
  16. for (std::vector<int>::iterator element = vec.begin(); element != vec.end(); ++element)
  17. std::cout << *element << std::endl;
  18. }

g++ -std=c++17 2.03.if.switch.cpp -o ifswitch


初始化列表 

  1. include <iostream>
  2. #include <initializer_list>
  3. #include <vector>
  4. class Foo {
  5. public:
  6. int value_a;
  7. int value_b;
  8. Foo(int a, int b) : value_a(a), value_b(b) {}
  9. };
  10. class MagicFoo {
  11. public:
  12. std::vector<int> vec;
  13. MagicFoo(std::initializer_list<int> list) {
  14. for (std::initializer_list<int>::iterator it = list.begin(); it != list.end(); ++it) {
  15. vec.push_back(*it);
  16. }
  17. }
  18. void foo(std::initializer_list<int> list) {
  19. for (std::initializer_list<int>::iterator it = list.begin(); it != list.end(); ++it) {
  20. vec.push_back(*it);
  21. }
  22. }
  23. };
  24. int main() {
  25. // before C++11
  26. int arr[3] = {1, 2, 3};
  27. Foo foo(1, 2);
  28. std::vector<int> vec = {1, 2, 3, 4, 5};
  29. // after C++11
  30. MagicFoo magicFoo = {1, 2, 3, 4, 5};
  31. magicFoo.foo({6,7,8,9});
  32. Foo foo2 {3, 4};
  33. std::cout << "arr[0]: " << arr[0] << std::endl;
  34. std::cout << "foo:" << foo.value_a << ", " << foo.value_b << std::endl;
  35. std::cout << "vec: ";
  36. for (std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {
  37. std::cout << *it << std::endl;
  38. }
  39. std::cout << "magicFoo: ";
  40. for (std::vector<int>::iterator it = magicFoo.vec.begin(); it != magicFoo.vec.end(); ++it) {
  41. std::cout << *it << std::endl;
  42. }
  43. std::cout << "foo2: " << foo2.value_a << ", " << foo2.value_b << std::endl;
  44. return 0;
  45. }

g++ -std=c++17 2.04.initializer.list.cpp -o initlist 


结构化绑定

编译原有程序报错:

查阅 github.com/changkun/modern-cpp-tutorial/issues/166

并未得到合理解决。

提供资料如下:

  1. en.cppreference.com/w/cpp/utility/tuple
  2. riptutorial.com/cplusplus/example/1601/using-std--tuple

g++ 5 不支持

 

g++ 11 支持

 

 clang 5 也支持

程序没有问题,是编译器版本有点旧。

  1. #include <iostream>
  2. #include <tuple>
  3. #include <string>
  4. std::tuple<int, double, std::string> f() {
  5. return std::make_tuple(1, 2.3, "456");
  6. }
  7. int main() {
  8. int x=0;
  9. double y=0.1;
  10. std::string z="123";
  11. std::cout << x << ", " << y << ", " << z << std::endl;
  12. std:tie(x, y, z) = f();
  13. std::cout << x << ", " << y << ", " << z << std::endl;
  14. return 0;
  15. }

 


  1. shiyanlou:~/ $ ls [14:33:08]
  2. anaconda3 Code Desktop
  3. shiyanlou:~/ $ history [14:33:20]
  4. 1 git clone https://github.com/changkun/modern-cpp-tutorial.git
  5. 2 g++ -std=c++17 2.03.if.switch.cpp -o ifswitch
  6. 3 ./ifswitch
  7. 4 g++ -std=c++17 2.04.initializer.list.cpp -o initlist
  8. 5 ./initlist
  9. 6 g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  10. 7 g++ -std=c++17 2.05.structured.binding.cpp -g -Wall -Wno-unused-variable -pthread -o strubind
  11. 8 g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  12. 9 g++ -std=c++17 2.05.structured.binding.cpp -g -Wall -Wno-unused-variable -pthread -o strubind
  13. 10 g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  14. 11 ./strubind
  15. 12 g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  16. 13 ./strubind
  17. 14 gcc -v
  18. 15 g++ -v
  19. 16 ls
  20. shiyanlou:~/ $ [14:33:24]

  1. shiyanlou:2/ (master) $ g++ -std=c++17 2.03.if.switch.cpp -o ifswitch
  2. 2.03.if.switch.cpp: In function \u2018int main()\u2019:
  3. 2.03.if.switch.cpp:23:84: error: expected \u2018)\u2019 before \u2018;\u2019 token
  4. f (const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 3);
  5. ^
  6. 2.03.if.switch.cpp:23:84: error: could not convert \u2018itr\u2019 from \u2018const iterator {aka const __gnu_cxx::__normal_iterator<int*, std::vector<int> >}\u2019 to \u2018bool\u2019
  7. 2.03.if.switch.cpp:24:25: error: expected \u2018;\u2019 before \u2018)\u2019 token
  8. itr != vec.end()) {
  9. ^
  10. shiyanlou:2/ (master) $ g++ -std=c++17 2.03.if.switch.cpp -o ifswitch
  11. 2.03.if.switch.cpp: In function \u2018int main()\u2019:
  12. 2.03.if.switch.cpp:23:38: error: redeclaration of \u2018const iterator itr\u2019
  13. const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 3)
  14. ^
  15. 2.03.if.switch.cpp:18:38: note: \u2018const iterator itr\u2019 previously declared here
  16. const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 2)
  17. ^
  18. shiyanlou:2/ (master*) $ g++ -std=c++17 2.03.if.switch.cpp -o ifswitch
  19. shiyanlou:2/ (master*) $ ./ifswitch [13:42:51]
  20. 1
  21. 4
  22. 3
  23. 4
  24. shiyanlou:2/ (master*) $ [13:43:02]
  25. shiyanlou:2/ (master*) $ g++ -std=c++17 2.04.initializer.list.cpp -o initlist
  26. shiyanlou:2/ (master*) $ ./initlist [13:46:31]
  27. arr[0]: 1
  28. foo:1, 2
  29. vec: 1
  30. 2
  31. 3
  32. 4
  33. 5
  34. magicFoo: 1
  35. 2
  36. 3
  37. 4
  38. 5
  39. 6
  40. 7
  41. 8
  42. 9
  43. foo2: 3, 4
  44. shiyanlou:2/ (master*) $ [13:46:38]
  45. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  46. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  47. 2.05.structured.binding.cpp:19:10: error: expected unqualified-id before \u2018[\u2019 token
  48. auto [x, y, z] = f();
  49. ^
  50. 2.05.structured.binding.cpp:20:18: error: \u2018x\u2019 was not declared in this scope
  51. std::cout << x << ", " << y << ", " << z << std::endl;
  52. ^
  53. 2.05.structured.binding.cpp:20:31: error: \u2018y\u2019 was not declared in this scope
  54. std::cout << x << ", " << y << ", " << z << std::endl;
  55. ^
  56. 2.05.structured.binding.cpp:20:44: error: \u2018z\u2019 was not declared in this scope
  57. std::cout << x << ", " << y << ", " << z << std::endl;
  58. ^
  59. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -g -Wall -Wno-unused-variable -pthread -o strubind
  60. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  61. 2.05.structured.binding.cpp:19:10: error: expected unqualified-id before \u2018[\u2019 token
  62. auto [x, y, z] = f();
  63. ^
  64. 2.05.structured.binding.cpp:20:18: error: \u2018x\u2019 was not declared in this scope
  65. std::cout << x << ", " << y << ", " << z << std::endl;
  66. ^
  67. 2.05.structured.binding.cpp:20:31: error: \u2018y\u2019 was not declared in this scope
  68. std::cout << x << ", " << y << ", " << z << std::endl;
  69. ^
  70. 2.05.structured.binding.cpp:20:44: error: \u2018z\u2019 was not declared in this scope
  71. std::cout << x << ", " << y << ", " << z << std::endl;
  72. ^
  73. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  74. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  75. 2.05.structured.binding.cpp:19:10: error: declaration of \u2018auto x\u2019 has no initializer
  76. auto x, y, z;
  77. ^
  78. 2.05.structured.binding.cpp:19:13: error: declaration of \u2018auto y\u2019 has no initializer
  79. auto x, y, z;
  80. ^
  81. 2.05.structured.binding.cpp:19:16: error: declaration of \u2018auto z\u2019 has no initializer
  82. auto x, y, z;
  83. ^
  84. 2.05.structured.binding.cpp: In lambda function:
  85. 2.05.structured.binding.cpp:20:15: error: expected \u2018{\u2019 before \u2018=\u2019 token
  86. [x, y, z] = f();
  87. ^
  88. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  89. 2.05.structured.binding.cpp:20:15: error: no match for \u2018operator=\u2019 (operand types are \u2018main()::<lambda()>\u2019 and \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019)
  90. 2.05.structured.binding.cpp:20:13: note: candidate: main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&) <deleted>
  91. [x, y, z] = f();
  92. ^
  93. 2.05.structured.binding.cpp:20:13: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018const main()::<lambda()>&\u2019
  94. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  95. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  96. 2.05.structured.binding.cpp:20:6: error: expected identifier before \u2018auto\u2019
  97. [auto x,auto y,auto z] = f();
  98. ^
  99. 2.05.structured.binding.cpp:20:12: error: expected \u2018]\u2019 before \u2018,\u2019 token
  100. [auto x,auto y,auto z] = f();
  101. ^
  102. 2.05.structured.binding.cpp: In lambda function:
  103. 2.05.structured.binding.cpp:20:12: error: expected \u2018{\u2019 before \u2018,\u2019 token
  104. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  105. 2.05.structured.binding.cpp:20:13: error: expected primary-expression before \u2018auto\u2019
  106. [auto x,auto y,auto z] = f();
  107. ^
  108. 2.05.structured.binding.cpp:21:18: error: \u2018x\u2019 was not declared in this scope
  109. std::cout << x << ", " << y << ", " << z << std::endl;
  110. ^
  111. 2.05.structured.binding.cpp:21:31: error: \u2018y\u2019 was not declared in this scope
  112. std::cout << x << ", " << y << ", " << z << std::endl;
  113. ^
  114. 2.05.structured.binding.cpp:21:44: error: \u2018z\u2019 was not declared in this scope
  115. std::cout << x << ", " << y << ", " << z << std::endl;
  116. ^
  117. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -g -Wall -Wno-unused-variable -pthread -o strubind
  118. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  119. 2.05.structured.binding.cpp:20:6: error: expected identifier before \u2018auto\u2019
  120. [auto x, auto y, auto z] = f();
  121. ^
  122. 2.05.structured.binding.cpp:20:12: error: expected \u2018]\u2019 before \u2018,\u2019 token
  123. [auto x, auto y, auto z] = f();
  124. ^
  125. 2.05.structured.binding.cpp: In lambda function:
  126. 2.05.structured.binding.cpp:20:12: error: expected \u2018{\u2019 before \u2018,\u2019 token
  127. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  128. 2.05.structured.binding.cpp:20:14: error: expected primary-expression before \u2018auto\u2019
  129. [auto x, auto y, auto z] = f();
  130. ^
  131. 2.05.structured.binding.cpp:21:18: error: \u2018x\u2019 was not declared in this scope
  132. std::cout << x << ", " << y << ", " << z << std::endl;
  133. ^
  134. 2.05.structured.binding.cpp:21:31: error: \u2018y\u2019 was not declared in this scope
  135. std::cout << x << ", " << y << ", " << z << std::endl;
  136. ^
  137. 2.05.structured.binding.cpp:21:44: error: \u2018z\u2019 was not declared in this scope
  138. std::cout << x << ", " << y << ", " << z << std::endl;
  139. ^
  140. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  141. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  142. 2.05.structured.binding.cpp:20:6: error: expected identifier before \u2018auto\u2019
  143. [auto x, auto y, auto z] = f();
  144. ^
  145. 2.05.structured.binding.cpp:20:12: error: expected \u2018]\u2019 before \u2018,\u2019 token
  146. [auto x, auto y, auto z] = f();
  147. ^
  148. 2.05.structured.binding.cpp: In lambda function:
  149. 2.05.structured.binding.cpp:20:12: error: expected \u2018{\u2019 before \u2018,\u2019 token
  150. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  151. 2.05.structured.binding.cpp:20:14: error: expected primary-expression before \u2018auto\u2019
  152. [auto x, auto y, auto z] = f();
  153. ^
  154. 2.05.structured.binding.cpp:21:18: error: \u2018x\u2019 was not declared in this scope
  155. std::cout << x << ", " << y << ", " << z << std::endl;
  156. ^
  157. 2.05.structured.binding.cpp:21:31: error: \u2018y\u2019 was not declared in this scope
  158. std::cout << x << ", " << y << ", " << z << std::endl;
  159. ^
  160. 2.05.structured.binding.cpp:21:44: error: \u2018z\u2019 was not declared in this scope
  161. std::cout << x << ", " << y << ", " << z << std::endl;
  162. ^
  163. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  164. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  165. 2.05.structured.binding.cpp:21:17: error: inconsistent deduction for \u2018auto\u2019: \u2018int\u2019 and then \u2018double\u2019
  166. auto x=0, y=0.1, z="123";
  167. ^
  168. 2.05.structured.binding.cpp:21:24: error: inconsistent deduction for \u2018auto\u2019: \u2018int\u2019 and then \u2018const char*\u2019
  169. auto x=0, y=0.1, z="123";
  170. ^
  171. 2.05.structured.binding.cpp: In lambda function:
  172. 2.05.structured.binding.cpp:23:15: error: expected \u2018{\u2019 before \u2018=\u2019 token
  173. [x, y, z] = f();
  174. ^
  175. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  176. 2.05.structured.binding.cpp:23:15: error: no match for \u2018operator=\u2019 (operand types are \u2018main()::<lambda()>\u2019 and \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019)
  177. 2.05.structured.binding.cpp:23:13: note: candidate: main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&) <deleted>
  178. [x, y, z] = f();
  179. ^
  180. 2.05.structured.binding.cpp:23:13: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018const main()::<lambda()>&\u2019
  181. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  182. 2.05.structured.binding.cpp: In lambda function:
  183. 2.05.structured.binding.cpp:25:15: error: expected \u2018{\u2019 before \u2018=\u2019 token
  184. [x, y, z] = f();
  185. ^
  186. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  187. 2.05.structured.binding.cpp:25:15: error: no match for \u2018operator=\u2019 (operand types are \u2018main()::<lambda()>\u2019 and \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019)
  188. 2.05.structured.binding.cpp:25:13: note: candidate: main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&) <deleted>
  189. [x, y, z] = f();
  190. ^
  191. 2.05.structured.binding.cpp:25:13: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018const main()::<lambda()>&\u2019
  192. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  193. 2.05.structured.binding.cpp: In lambda function:
  194. 2.05.structured.binding.cpp:25:15: error: expected \u2018{\u2019 before \u2018=\u2019 token
  195. [x, y, z] = f();
  196. ^
  197. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  198. 2.05.structured.binding.cpp:25:15: error: no match for \u2018operator=\u2019 (operand types are \u2018main()::<lambda()>\u2019 and \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019)
  199. 2.05.structured.binding.cpp:25:13: note: candidate: main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&) <deleted>
  200. [x, y, z] = f();
  201. ^
  202. 2.05.structured.binding.cpp:25:13: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018const main()::<lambda()>&\u2019
  203. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  204. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  205. 2.05.structured.binding.cpp:25:16: error: the value of \u2018x\u2019 is not usable in a constant expression
  206. std::tuple<x, y, z> = f();
  207. ^
  208. 2.05.structured.binding.cpp:21:10: note: \u2018int x\u2019 is not const
  209. auto x=0;
  210. ^
  211. 2.05.structured.binding.cpp:25:19: error: the value of \u2018y\u2019 is not usable in a constant expression
  212. std::tuple<x, y, z> = f();
  213. ^
  214. 2.05.structured.binding.cpp:22:10: note: \u2018y\u2019 was not declared \u2018constexpr\u2019
  215. auto y=0.1;
  216. ^
  217. 2.05.structured.binding.cpp:25:22: error: the value of \u2018z\u2019 is not usable in a constant expression
  218. std::tuple<x, y, z> = f();
  219. ^
  220. 2.05.structured.binding.cpp:23:10: note: \u2018z\u2019 was not declared \u2018constexpr\u2019
  221. auto z="123";
  222. ^
  223. 2.05.structured.binding.cpp:25:23: error: type/value mismatch at argument 1 in template parameter list for \u2018template<class ...> class std::tuple\u2019
  224. std::tuple<x, y, z> = f();
  225. ^
  226. 2.05.structured.binding.cpp:25:23: note: expected a type, got \u2018x\u2019
  227. 2.05.structured.binding.cpp:25:23: error: type/value mismatch at argument 1 in template parameter list for \u2018template<class ...> class std::tuple\u2019
  228. 2.05.structured.binding.cpp:25:23: note: expected a type, got \u2018y\u2019
  229. 2.05.structured.binding.cpp:25:23: error: type/value mismatch at argument 1 in template parameter list for \u2018template<class ...> class std::tuple\u2019
  230. 2.05.structured.binding.cpp:25:23: note: expected a type, got \u2018z\u2019
  231. 2.05.structured.binding.cpp:25:25: error: expected unqualified-id before \u2018=\u2019 token
  232. std::tuple<x, y, z> = f();
  233. ^
  234. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  235. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  236. 2.05.structured.binding.cpp:25:15: error: cannot convert \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018const char*\u2019 in assignment
  237. (x, y, z) = f();
  238. ^
  239. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  240. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  241. 2.05.structured.binding.cpp:25:15: error: no match for \u2018operator=\u2019 (operand types are \u2018std::__cxx11::string {aka std::__cxx11::basic_string<char>}\u2019 and \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019)
  242. (x, y, z) = f();
  243. ^
  244. In file included from /usr/include/c++/5/string:52:0,
  245. from /usr/include/c++/5/bits/locale_classes.h:40,
  246. from /usr/include/c++/5/bits/ios_base.h:41,
  247. from /usr/include/c++/5/ios:42,
  248. from /usr/include/c++/5/ostream:38,
  249. from /usr/include/c++/5/iostream:39,
  250. from 2.05.structured.binding.cpp:10:
  251. /usr/include/c++/5/bits/basic_string.h:550:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
  252. operator=(const basic_string& __str)
  253. ^
  254. /usr/include/c++/5/bits/basic_string.h:550:7: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018const std::__cxx11::basic_string<char>&\u2019
  255. /usr/include/c++/5/bits/basic_string.h:558:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
  256. operator=(const _CharT* __s)
  257. ^
  258. /usr/include/c++/5/bits/basic_string.h:558:7: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018const char*\u2019
  259. /usr/include/c++/5/bits/basic_string.h:569:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
  260. operator=(_CharT __c)
  261. ^
  262. /usr/include/c++/5/bits/basic_string.h:569:7: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018char\u2019
  263. /usr/include/c++/5/bits/basic_string.h:587:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
  264. operator=(basic_string&& __str)
  265. ^
  266. /usr/include/c++/5/bits/basic_string.h:587:7: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018std::__cxx11::basic_string<char>&&\u2019
  267. /usr/include/c++/5/bits/basic_string.h:598:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(std::initializer_list<_Tp>) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
  268. operator=(initializer_list<_CharT> __l)
  269. ^
  270. /usr/include/c++/5/bits/basic_string.h:598:7: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018std::initializer_list<char>\u2019
  271. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  272. 2.05.structured.binding.cpp: In lambda function:
  273. 2.05.structured.binding.cpp:25:15: error: expected \u2018{\u2019 before \u2018=\u2019 token
  274. [x, y, z] = f();
  275. ^
  276. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  277. 2.05.structured.binding.cpp:25:15: error: no match for \u2018operator=\u2019 (operand types are \u2018main()::<lambda()>\u2019 and \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019)
  278. 2.05.structured.binding.cpp:25:13: note: candidate: main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&) <deleted>
  279. [x, y, z] = f();
  280. ^
  281. 2.05.structured.binding.cpp:25:13: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018const main()::<lambda()>&\u2019
  282. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  283. shiyanlou:2/ (master*) $ ./strubind [14:13:51]
  284. 0, 0.1, 123
  285. 0, 0.1, 123
  286. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  287. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  288. 2.05.structured.binding.cpp:25:16: error: the value of \u2018x\u2019 is not usable in a constant expression
  289. std::tuple<x, y, z> = f();
  290. ^
  291. 2.05.structured.binding.cpp:21:9: note: \u2018int x\u2019 is not const
  292. int x=0;
  293. ^
  294. 2.05.structured.binding.cpp:25:19: error: the value of \u2018y\u2019 is not usable in a constant expression
  295. std::tuple<x, y, z> = f();
  296. ^
  297. 2.05.structured.binding.cpp:22:12: note: \u2018y\u2019 was not declared \u2018constexpr\u2019
  298. double y=0.1;
  299. ^
  300. 2.05.structured.binding.cpp:25:22: error: the value of \u2018z\u2019 is not usable in a constant expression
  301. std::tuple<x, y, z> = f();
  302. ^
  303. 2.05.structured.binding.cpp:23:17: note: \u2018z\u2019 was not declared \u2018constexpr\u2019
  304. std::string z="123";
  305. ^
  306. 2.05.structured.binding.cpp:25:23: error: type/value mismatch at argument 1 in template parameter list for \u2018template<class ...> class std::tuple\u2019
  307. std::tuple<x, y, z> = f();
  308. ^
  309. 2.05.structured.binding.cpp:25:23: note: expected a type, got \u2018x\u2019
  310. 2.05.structured.binding.cpp:25:23: error: type/value mismatch at argument 1 in template parameter list for \u2018template<class ...> class std::tuple\u2019
  311. 2.05.structured.binding.cpp:25:23: note: expected a type, got \u2018y\u2019
  312. 2.05.structured.binding.cpp:25:23: error: type/value mismatch at argument 1 in template parameter list for \u2018template<class ...> class std::tuple\u2019
  313. 2.05.structured.binding.cpp:25:23: note: expected a type, got \u2018z\u2019
  314. 2.05.structured.binding.cpp:25:25: error: expected unqualified-id before \u2018=\u2019 token
  315. std::tuple<x, y, z> = f();
  316. ^
  317. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  318. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  319. 2.05.structured.binding.cpp:25:5: error: expected primary-expression before \u2018<\u2019 token
  320. <x, y, z> = f();
  321. ^
  322. 2.05.structured.binding.cpp:25:15: error: expected primary-expression before \u2018=\u2019 token
  323. <x, y, z> = f();
  324. ^
  325. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  326. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  327. 2.05.structured.binding.cpp:25:15: error: no match for \u2018operator=\u2019 (operand types are \u2018std::__cxx11::string {aka std::__cxx11::basic_string<char>}\u2019 and \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019)
  328. (x, y, z) = f();
  329. ^
  330. In file included from /usr/include/c++/5/string:52:0,
  331. from /usr/include/c++/5/bits/locale_classes.h:40,
  332. from /usr/include/c++/5/bits/ios_base.h:41,
  333. from /usr/include/c++/5/ios:42,
  334. from /usr/include/c++/5/ostream:38,
  335. from /usr/include/c++/5/iostream:39,
  336. from 2.05.structured.binding.cpp:10:
  337. /usr/include/c++/5/bits/basic_string.h:550:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
  338. operator=(const basic_string& __str)
  339. ^
  340. /usr/include/c++/5/bits/basic_string.h:550:7: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018const std::__cxx11::basic_string<char>&\u2019
  341. /usr/include/c++/5/bits/basic_string.h:558:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(const _CharT*) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
  342. operator=(const _CharT* __s)
  343. ^
  344. /usr/include/c++/5/bits/basic_string.h:558:7: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018const char*\u2019
  345. /usr/include/c++/5/bits/basic_string.h:569:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(_CharT) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
  346. operator=(_CharT __c)
  347. ^
  348. /usr/include/c++/5/bits/basic_string.h:569:7: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018char\u2019
  349. /usr/include/c++/5/bits/basic_string.h:587:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
  350. operator=(basic_string&& __str)
  351. ^
  352. /usr/include/c++/5/bits/basic_string.h:587:7: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018std::__cxx11::basic_string<char>&&\u2019
  353. /usr/include/c++/5/bits/basic_string.h:598:7: note: candidate: std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(std::initializer_list<_Tp>) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]
  354. operator=(initializer_list<_CharT> __l)
  355. ^
  356. /usr/include/c++/5/bits/basic_string.h:598:7: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018std::initializer_list<char>\u2019
  357. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  358. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  359. 2.05.structured.binding.cpp:25:13: error: expected \u2018;\u2019 before \u2018}\u2019 token
  360. {x, y, z} = f();
  361. ^
  362. 2.05.structured.binding.cpp:25:15: error: expected primary-expression before \u2018=\u2019 token
  363. {x, y, z} = f();
  364. ^
  365. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  366. 2.05.structured.binding.cpp: In lambda function:
  367. 2.05.structured.binding.cpp:25:15: error: expected \u2018{\u2019 before \u2018=\u2019 token
  368. [x, y, z] = f();
  369. ^
  370. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  371. 2.05.structured.binding.cpp:25:15: error: no match for \u2018operator=\u2019 (operand types are \u2018main()::<lambda()>\u2019 and \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019)
  372. 2.05.structured.binding.cpp:25:13: note: candidate: main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&) <deleted>
  373. [x, y, z] = f();
  374. ^
  375. 2.05.structured.binding.cpp:25:13: note: no known conversion for argument 1 from \u2018std::tuple<int, double, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >\u2019 to \u2018const main()::<lambda()>&\u2019
  376. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  377. 2.05.structured.binding.cpp: In function \u2018int main()\u2019:
  378. 2.05.structured.binding.cpp:25:10: error: expected unqualified-id before \u2018[\u2019 token
  379. auto [x, y, z] = f();
  380. ^
  381. shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
  382. shiyanlou:2/ (master*) $ ./strubind [14:22:38]
  383. 0, 0.1, 123
  384. 1, 2.3, 456
  385. shiyanlou:2/ (master*) $ gcc -v [14:22:41]
  386. Using built-in specs.
  387. COLLECT_GCC=gcc
  388. COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
  389. Target: x86_64-linux-gnu
  390. Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.9' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
  391. Thread model: posix
  392. gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
  393. shiyanlou:2/ (master*) $ g++ -v [14:28:11]
  394. Using built-in specs.
  395. COLLECT_GCC=g++
  396. COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
  397. Target: x86_64-linux-gnu
  398. Configured with: ../src/configure -v --with-pkgversion='Ubuntu 5.4.0-6ubuntu1~16.04.9' --with-bugurl=file:///usr/share/doc/gcc-5/README.Bugs --enable-languages=c,ada,c++,java,go,d,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-5 --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-libmpx --enable-plugin --with-system-zlib --disable-browser-plugin --enable-java-awt=gtk --enable-gtk-cairo --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-5-amd64/jre --enable-java-home --with-jvm-root-dir=/usr/lib/jvm/java-1.5.0-gcj-5-amd64 --with-jvm-jar-dir=/usr/lib/jvm-exports/java-1.5.0-gcj-5-amd64 --with-arch-directory=amd64 --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --enable-objc-gc --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
  399. Thread model: posix
  400. gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9)
  401. shiyanlou:2/ (master*) $ [14:28:26]

 


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/104836
推荐阅读
相关标签
  

闽ICP备14008679号