Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,039 questions

52,004 answers

573 users

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 178 views
178 views asked Feb 12, 2019 by avibootz
1 answer 126 views
126 views asked May 11, 2018 by avibootz
1 answer 157 views
4 answers 263 views
1 answer 159 views
...