How to create and pass tuple to a constructor in C++

1 Answer

0 votes
#include <iostream>
#include <utility>
#include <tuple>

class Test 
{
	public:
		Test(std::tuple<int, float>) {
			std::cout << "Test::Test(tuple)" << std::endl;
		}
		template <typename... Args>
		Test(Args... args) {
			std::cout << "Test::Test(args...)" << std::endl;
		}
};

int main()
{
	// create tuple
	std::tuple<int, float> tpl(98, 3.14);

	// pass tuple to the constructor
	std::pair<int, Test> pr1(185, tpl);

	// pass tuple elements to the constructor
	std::pair<int, Test> pr2(std::piecewise_construct, std::make_tuple(185), tpl);

	return 0;
}


/*
run:

Test::Test(tuple)
Test::Test(args...)

*/

 



answered Nov 25, 2017 by avibootz

Related questions

2 answers 188 views
188 views asked Feb 12, 2019 by avibootz
1 answer 141 views
141 views asked May 11, 2018 by avibootz
1 answer 168 views
4 answers 281 views
1 answer 169 views
...