I am curious about the principle of variable in c++ boost library.

Asked 2 years ago, Updated 2 years ago, 139 views

Hello, I'm a student studying c++.++.

In my project, I need a class with functions similar to the variable in boost.

However, the cost is too high to add boost, so I tried to implement it myself, although it was sloppy.

So I opened the boost variable.hpp and saw it, but I couldn't understand it, so I posted a question like this.

I am curious about the principle of variable and what is the most important c++ grammar for implementing variable!

c++ boost

2022-09-21 17:06

1 Answers

If you can use C++11 or higher, you can use the triple.

http://www.cplusplus.com/reference/tuple/tuple/

// http://www.cplusplus.com/reference/tuple/tuple/
// // tuple example
#include <iostream>     // std::cout
#include <tuple>        // std::tuple, std::get, std::tie, std::ignore

int main ()
{
  std::tuple<int,char> foo (10,'x');
  auto bar = std::make_tuple ("test", 3.1, 14, 'y');

  std::get<2>(bar) = 100;                                    // access element

  int myint; char mychar;

  std::tie (myint, mychar) = foo;                            // unpack elements
  std::tie (std::ignore, std::ignore, myint, mychar) = bar;  // unpack (with ignore)

  mychar = std::get<3>(bar);

  std::get<0>(foo) = std::get<2>(bar);
  std::get<1>(foo) = mychar;

  std::cout << "foo contains: ";
  std::cout << std::get<0>(foo) << ' ';
  std::cout << std::get<1>(foo) << '\n';

  return 0;
}


2022-09-21 17:06

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.