前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >蓝桥ROS机器人之现代C++学习笔记2.2变量及其初始化

蓝桥ROS机器人之现代C++学习笔记2.2变量及其初始化

作者头像
zhangrelay
发布2022-04-29 19:32:42
3390
发布2022-04-29 19:32:42
举报

if/switch 变量声明强化

代码语言:javascript
复制
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {1, 2, 3, 4};

    // after c++17, can be simplefied by using `auto`
    const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 2);
    if (itr != vec.end()) {
        *itr = 3;
    }

    const std::vector<int>::iterator itr1 = std::find(vec.begin(), vec.end(), 3);
    if (itr1 != vec.end()) {
        *itr1 = 4;
    }

    // should output: 1, 4, 3, 4. can be simplefied using `auto`
    for (std::vector<int>::iterator element = vec.begin(); element != vec.end(); ++element)
        std::cout << *element << std::endl;
}

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


初始化列表 

代码语言:javascript
复制
include <iostream>
#include <initializer_list>
#include <vector>

class Foo {
public:
    int value_a;
    int value_b;
    Foo(int a, int b) : value_a(a), value_b(b) {}
};

class MagicFoo {
public:
    std::vector<int> vec;
    MagicFoo(std::initializer_list<int> list) {
        for (std::initializer_list<int>::iterator it = list.begin(); it != list.end(); ++it) {
            vec.push_back(*it);
        }
    }
    void foo(std::initializer_list<int> list) {
        for (std::initializer_list<int>::iterator it = list.begin(); it != list.end(); ++it) {
            vec.push_back(*it);
        }
    }
};

int main() {
    // before C++11
    int arr[3] = {1, 2, 3};
    Foo foo(1, 2);
    std::vector<int> vec = {1, 2, 3, 4, 5};

    // after C++11
    MagicFoo magicFoo = {1, 2, 3, 4, 5};
    magicFoo.foo({6,7,8,9});
    Foo foo2 {3, 4};

    std::cout << "arr[0]: " << arr[0] << std::endl;
    std::cout << "foo:" << foo.value_a << ", " << foo.value_b << std::endl;
    std::cout << "vec: ";
    for (std::vector<int>::iterator it = vec.begin(); it != vec.end(); ++it) {
        std::cout << *it << std::endl;
    }
    std::cout << "magicFoo: ";
    for (std::vector<int>::iterator it = magicFoo.vec.begin(); it != magicFoo.vec.end(); ++it) {
        std::cout << *it << std::endl;
    }
    std::cout << "foo2: " << foo2.value_a << ", " << foo2.value_b << std::endl;

    return 0;
}

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 也支持

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

代码语言:javascript
复制
#include <iostream>
#include <tuple>
#include <string>

std::tuple<int, double, std::string> f() {
    return std::make_tuple(1, 2.3, "456");
}

int main() {
    int x=0; 
    double y=0.1;
    std::string z="123";
    std::cout << x << ", " << y << ", " << z << std::endl;
    std:tie(x, y, z) = f();
    std::cout << x << ", " << y << ", " << z << std::endl;
    return 0;
}

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

代码语言:javascript
复制
shiyanlou:2/ (master) $ g++ -std=c++17 2.03.if.switch.cpp -o ifswitch 
2.03.if.switch.cpp: In function \u2018int main()\u2019:
2.03.if.switch.cpp:23:84: error: expected \u2018)\u2019 before \u2018;\u2019 token
 f (const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 3);
                                                                               ^
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
2.03.if.switch.cpp:24:25: error: expected \u2018;\u2019 before \u2018)\u2019 token
         itr != vec.end()) {
                         ^
shiyanlou:2/ (master) $ g++ -std=c++17 2.03.if.switch.cpp -o ifswitch
2.03.if.switch.cpp: In function \u2018int main()\u2019:
2.03.if.switch.cpp:23:38: error: redeclaration of \u2018const iterator itr\u2019
     const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 3)
                                      ^
2.03.if.switch.cpp:18:38: note: \u2018const iterator itr\u2019 previously declared here
     const std::vector<int>::iterator itr = std::find(vec.begin(), vec.end(), 2)
                                      ^
shiyanlou:2/ (master*) $ g++ -std=c++17 2.03.if.switch.cpp -o ifswitch
shiyanlou:2/ (master*) $ ./ifswitch                                  [13:42:51]
1
4
3
4
shiyanlou:2/ (master*) $                                             [13:43:02]
shiyanlou:2/ (master*) $ g++ -std=c++17 2.04.initializer.list.cpp -o initlist
shiyanlou:2/ (master*) $ ./initlist                                  [13:46:31]
arr[0]: 1
foo:1, 2
vec: 1
2
3
4
5
magicFoo: 1
2
3
4
5
6
7
8
9
foo2: 3, 4
shiyanlou:2/ (master*) $                                             [13:46:38]
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
2.05.structured.binding.cpp:19:10: error: expected unqualified-id before \u2018[\u2019 token
     auto [x, y, z] = f();
          ^
2.05.structured.binding.cpp:20:18: error: \u2018x\u2019 was not declared in this scope
     std::cout << x << ", " << y << ", " << z << std::endl;
                  ^
2.05.structured.binding.cpp:20:31: error: \u2018y\u2019 was not declared in this scope
     std::cout << x << ", " << y << ", " << z << std::endl;
                               ^
2.05.structured.binding.cpp:20:44: error: \u2018z\u2019 was not declared in this scope
     std::cout << x << ", " << y << ", " << z << std::endl;
                                            ^
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -g -Wall -Wno-unused-variable -pthread -o strubind
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
2.05.structured.binding.cpp:19:10: error: expected unqualified-id before \u2018[\u2019 token
     auto [x, y, z] = f();
          ^
2.05.structured.binding.cpp:20:18: error: \u2018x\u2019 was not declared in this scope
     std::cout << x << ", " << y << ", " << z << std::endl;
                  ^
2.05.structured.binding.cpp:20:31: error: \u2018y\u2019 was not declared in this scope
     std::cout << x << ", " << y << ", " << z << std::endl;
                               ^
2.05.structured.binding.cpp:20:44: error: \u2018z\u2019 was not declared in this scope
     std::cout << x << ", " << y << ", " << z << std::endl;
                                            ^
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
2.05.structured.binding.cpp:19:10: error: declaration of \u2018auto x\u2019 has no initializer
     auto x, y, z;
          ^
2.05.structured.binding.cpp:19:13: error: declaration of \u2018auto y\u2019 has no initializer
     auto x, y, z;
             ^
2.05.structured.binding.cpp:19:16: error: declaration of \u2018auto z\u2019 has no initializer
     auto x, y, z;
                ^
2.05.structured.binding.cpp: In lambda function:
2.05.structured.binding.cpp:20:15: error: expected \u2018{\u2019 before \u2018=\u2019 token
     [x, y, z] = f();
               ^
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
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)
2.05.structured.binding.cpp:20:13: note: candidate: main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&) <deleted>
     [x, y, z] = f();
             ^
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
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
2.05.structured.binding.cpp:20:6: error: expected identifier before \u2018auto\u2019
     [auto x,auto y,auto z] = f();
      ^
2.05.structured.binding.cpp:20:12: error: expected \u2018]\u2019 before \u2018,\u2019 token
     [auto x,auto y,auto z] = f();
            ^
2.05.structured.binding.cpp: In lambda function:
2.05.structured.binding.cpp:20:12: error: expected \u2018{\u2019 before \u2018,\u2019 token
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
2.05.structured.binding.cpp:20:13: error: expected primary-expression before \u2018auto\u2019
     [auto x,auto y,auto z] = f();
             ^
2.05.structured.binding.cpp:21:18: error: \u2018x\u2019 was not declared in this scope
     std::cout << x << ", " << y << ", " << z << std::endl;
                  ^
2.05.structured.binding.cpp:21:31: error: \u2018y\u2019 was not declared in this scope
     std::cout << x << ", " << y << ", " << z << std::endl;
                               ^
2.05.structured.binding.cpp:21:44: error: \u2018z\u2019 was not declared in this scope
     std::cout << x << ", " << y << ", " << z << std::endl;
                                            ^
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -g -Wall -Wno-unused-variable -pthread -o strubind
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
2.05.structured.binding.cpp:20:6: error: expected identifier before \u2018auto\u2019
     [auto x, auto y, auto z] = f();
      ^
2.05.structured.binding.cpp:20:12: error: expected \u2018]\u2019 before \u2018,\u2019 token
     [auto x, auto y, auto z] = f();
            ^
2.05.structured.binding.cpp: In lambda function:
2.05.structured.binding.cpp:20:12: error: expected \u2018{\u2019 before \u2018,\u2019 token
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
2.05.structured.binding.cpp:20:14: error: expected primary-expression before \u2018auto\u2019
     [auto x, auto y, auto z] = f();
              ^
2.05.structured.binding.cpp:21:18: error: \u2018x\u2019 was not declared in this scope
     std::cout << x << ", " << y << ", " << z << std::endl;
                  ^
2.05.structured.binding.cpp:21:31: error: \u2018y\u2019 was not declared in this scope
     std::cout << x << ", " << y << ", " << z << std::endl;
                               ^
2.05.structured.binding.cpp:21:44: error: \u2018z\u2019 was not declared in this scope
     std::cout << x << ", " << y << ", " << z << std::endl;
                                            ^
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
2.05.structured.binding.cpp:20:6: error: expected identifier before \u2018auto\u2019
     [auto x, auto y, auto z] = f();
      ^
2.05.structured.binding.cpp:20:12: error: expected \u2018]\u2019 before \u2018,\u2019 token
     [auto x, auto y, auto z] = f();
            ^
2.05.structured.binding.cpp: In lambda function:
2.05.structured.binding.cpp:20:12: error: expected \u2018{\u2019 before \u2018,\u2019 token
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
2.05.structured.binding.cpp:20:14: error: expected primary-expression before \u2018auto\u2019
     [auto x, auto y, auto z] = f();
              ^
2.05.structured.binding.cpp:21:18: error: \u2018x\u2019 was not declared in this scope
     std::cout << x << ", " << y << ", " << z << std::endl;
                  ^
2.05.structured.binding.cpp:21:31: error: \u2018y\u2019 was not declared in this scope
     std::cout << x << ", " << y << ", " << z << std::endl;
                               ^
2.05.structured.binding.cpp:21:44: error: \u2018z\u2019 was not declared in this scope
     std::cout << x << ", " << y << ", " << z << std::endl;
                                            ^
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
2.05.structured.binding.cpp:21:17: error: inconsistent deduction for \u2018auto\u2019: \u2018int\u2019 and then \u2018double\u2019
     auto x=0, y=0.1, z="123";
                 ^
2.05.structured.binding.cpp:21:24: error: inconsistent deduction for \u2018auto\u2019: \u2018int\u2019 and then \u2018const char*\u2019
     auto x=0, y=0.1, z="123";
                        ^
2.05.structured.binding.cpp: In lambda function:
2.05.structured.binding.cpp:23:15: error: expected \u2018{\u2019 before \u2018=\u2019 token
     [x, y, z] = f();
               ^
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
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)
2.05.structured.binding.cpp:23:13: note: candidate: main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&) <deleted>
     [x, y, z] = f();
             ^
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
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In lambda function:
2.05.structured.binding.cpp:25:15: error: expected \u2018{\u2019 before \u2018=\u2019 token
     [x, y, z] = f();
               ^
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
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)
2.05.structured.binding.cpp:25:13: note: candidate: main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&) <deleted>
     [x, y, z] = f();
             ^
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
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In lambda function:
2.05.structured.binding.cpp:25:15: error: expected \u2018{\u2019 before \u2018=\u2019 token
     [x, y, z] = f();
               ^
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
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)
2.05.structured.binding.cpp:25:13: note: candidate: main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&) <deleted>
     [x, y, z] = f();
             ^
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
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
2.05.structured.binding.cpp:25:16: error: the value of \u2018x\u2019 is not usable in a constant expression
     std::tuple<x, y, z> = f();
                ^
2.05.structured.binding.cpp:21:10: note: \u2018int x\u2019 is not const
     auto x=0; 
          ^
2.05.structured.binding.cpp:25:19: error: the value of \u2018y\u2019 is not usable in a constant expression
     std::tuple<x, y, z> = f();
                   ^
2.05.structured.binding.cpp:22:10: note: \u2018y\u2019 was not declared \u2018constexpr\u2019
     auto y=0.1;
          ^
2.05.structured.binding.cpp:25:22: error: the value of \u2018z\u2019 is not usable in a constant expression
     std::tuple<x, y, z> = f();
                      ^
2.05.structured.binding.cpp:23:10: note: \u2018z\u2019 was not declared \u2018constexpr\u2019
     auto z="123";
          ^
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
     std::tuple<x, y, z> = f();
                       ^
2.05.structured.binding.cpp:25:23: note:   expected a type, got \u2018x\u2019
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
2.05.structured.binding.cpp:25:23: note:   expected a type, got \u2018y\u2019
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
2.05.structured.binding.cpp:25:23: note:   expected a type, got \u2018z\u2019
2.05.structured.binding.cpp:25:25: error: expected unqualified-id before \u2018=\u2019 token
     std::tuple<x, y, z> = f();
                         ^
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
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
     (x, y, z) = f();
               ^
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
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)
     (x, y, z) = f();
               ^
In file included from /usr/include/c++/5/string:52:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from 2.05.structured.binding.cpp:10:
/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>]
       operator=(const basic_string& __str)
       ^
/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
/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>]
       operator=(const _CharT* __s)
       ^
/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
/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>]
       operator=(_CharT __c)
       ^
/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
/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>]
       operator=(basic_string&& __str)
       ^
/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
/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>]
       operator=(initializer_list<_CharT> __l)
       ^
/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
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In lambda function:
2.05.structured.binding.cpp:25:15: error: expected \u2018{\u2019 before \u2018=\u2019 token
     [x, y, z] = f();
               ^
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
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)
2.05.structured.binding.cpp:25:13: note: candidate: main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&) <deleted>
     [x, y, z] = f();
             ^
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
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
shiyanlou:2/ (master*) $ ./strubind                                  [14:13:51]
0, 0.1, 123
0, 0.1, 123
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
2.05.structured.binding.cpp:25:16: error: the value of \u2018x\u2019 is not usable in a constant expression
     std::tuple<x, y, z> = f();
                ^
2.05.structured.binding.cpp:21:9: note: \u2018int x\u2019 is not const
     int x=0; 
         ^
2.05.structured.binding.cpp:25:19: error: the value of \u2018y\u2019 is not usable in a constant expression
     std::tuple<x, y, z> = f();
                   ^
2.05.structured.binding.cpp:22:12: note: \u2018y\u2019 was not declared \u2018constexpr\u2019
     double y=0.1;
            ^
2.05.structured.binding.cpp:25:22: error: the value of \u2018z\u2019 is not usable in a constant expression
     std::tuple<x, y, z> = f();
                      ^
2.05.structured.binding.cpp:23:17: note: \u2018z\u2019 was not declared \u2018constexpr\u2019
     std::string z="123";
                 ^
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
     std::tuple<x, y, z> = f();
                       ^
2.05.structured.binding.cpp:25:23: note:   expected a type, got \u2018x\u2019
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
2.05.structured.binding.cpp:25:23: note:   expected a type, got \u2018y\u2019
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
2.05.structured.binding.cpp:25:23: note:   expected a type, got \u2018z\u2019
2.05.structured.binding.cpp:25:25: error: expected unqualified-id before \u2018=\u2019 token
     std::tuple<x, y, z> = f();
                         ^
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
2.05.structured.binding.cpp:25:5: error: expected primary-expression before \u2018<\u2019 token
     <x, y, z> = f();
     ^
2.05.structured.binding.cpp:25:15: error: expected primary-expression before \u2018=\u2019 token
     <x, y, z> = f();
               ^
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
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)
     (x, y, z) = f();
               ^
In file included from /usr/include/c++/5/string:52:0,
                 from /usr/include/c++/5/bits/locale_classes.h:40,
                 from /usr/include/c++/5/bits/ios_base.h:41,
                 from /usr/include/c++/5/ios:42,
                 from /usr/include/c++/5/ostream:38,
                 from /usr/include/c++/5/iostream:39,
                 from 2.05.structured.binding.cpp:10:
/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>]
       operator=(const basic_string& __str)
       ^
/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
/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>]
       operator=(const _CharT* __s)
       ^
/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
/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>]
       operator=(_CharT __c)
       ^
/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
/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>]
       operator=(basic_string&& __str)
       ^
/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
/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>]
       operator=(initializer_list<_CharT> __l)
       ^
/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
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
2.05.structured.binding.cpp:25:13: error: expected \u2018;\u2019 before \u2018}\u2019 token
     {x, y, z} = f();
             ^
2.05.structured.binding.cpp:25:15: error: expected primary-expression before \u2018=\u2019 token
     {x, y, z} = f();
               ^
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In lambda function:
2.05.structured.binding.cpp:25:15: error: expected \u2018{\u2019 before \u2018=\u2019 token
     [x, y, z] = f();
               ^
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
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)
2.05.structured.binding.cpp:25:13: note: candidate: main()::<lambda()>& main()::<lambda()>::operator=(const main()::<lambda()>&) <deleted>
     [x, y, z] = f();
             ^
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
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
2.05.structured.binding.cpp: In function \u2018int main()\u2019:
2.05.structured.binding.cpp:25:10: error: expected unqualified-id before \u2018[\u2019 token
     auto [x, y, z] = f();
          ^
shiyanlou:2/ (master*) $ g++ -std=c++17 2.05.structured.binding.cpp -o strubind
shiyanlou:2/ (master*) $ ./strubind                                  [14:22:38]
0, 0.1, 123
1, 2.3, 456
shiyanlou:2/ (master*) $ gcc -v                                      [14:22:41]
Using built-in specs.
COLLECT_GCC=gcc
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
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
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9) 
shiyanlou:2/ (master*) $ g++ -v                                      [14:28:11]
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/5/lto-wrapper
Target: x86_64-linux-gnu
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
Thread model: posix
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.9) 
shiyanlou:2/ (master*) $                                             [14:28:26]
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2022-03-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档