Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

If I try to compile the example below i get the error

'std::tuple<long &,int>::tuple': no appropriate default constructor available

How can i make this example work in order to make tuple accept mixed lvalue ref and rValue?

C++
#include "stdafx.h"
#include <tuple>


template<class ...Arg>
class A
{
	std::tuple<Arg...> tuple_;
public:
	A(Arg&&...args)
	{
		tuple_ = std::make_tuple(std::forward<Arg>(args)...);
		
	}
};

template<class...Arg>
void f(Arg&&...args)
{
	A<Arg...> a(std::forward<Arg>(args)...);
}


int main()
{
    long t = 5;
    f(t,5);
	
    return 0;
}


What I have tried:

I tried using std::make_tuple , std::forward_as_tuple or std::tie, but nothing makes the example compile without error.
Posted
Updated 19-Jul-20 4:01am
v4

1 solution

It works (At least on my system) this way:
C++
#include <tuple>


template<class ...Arg>
class A
{
  std::tuple<Arg...> tuple_;
public:
  A(Arg&&...args):tuple_(std::forward<Arg>(args)...)
  {
  }
};

template<class...Arg>
void f(Arg&&...args)
{
  A<Arg...> a(std::forward<Arg>(args)...);
}


int main()
{
    long t = 5;
    f(t,5);

    return 0;
}


I guess (just a wild guess though) the reason beign such tuple ctor is (conditionally) explicit, see std::tuple<types...>::tuple[^].
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900