I've a template structure like this
template<typename ... Args> struct A;
template<>
struct A<>{
};
template<typename First, typename ... Args>
struct A<First, Args...>:private A<Args...>{
typedef A<Args...> child;
typedef A<First, Args...> self;
typedef First type;
static const size_t size = 1 + sizeof...(Args);
type value;
template<typename ... _Args>
A(First && first, _Args && ... args) :child(forward<_Args>(args)...),value(move(first)){}//move
template<typename ... _Args>
A(const First & first, _Args && ... args) :child(forward<_Args>(args)...), value(first){}//assign
};
If I call this structure like below;
int f= 6;
A<int, int, int> g(f, 5, f); //it is working
A<int&> i(f);//not working compile error
Second one not working cause rvalue constructor can't assign first to value.
What should I do for second one work?
Your bigger problem is that when First
is a lvalue reference (say, T &
), both First &&
and const First &
becomes T &
, meaning that your two constructors have the same signature, which is obviously illegal. (const First &
is "reference to const First
", but since references themselves are immutable , when First
is a reference type itself the const is meaningless.)
One possible fix would be making your constructor take universal references for the first parameter as well, i.e.:
template<typename F, typename ... Cargs>
A(F&& first, Cargs && ... args) :child(forward<Cargs>(args)...),value(forward<F>(first)){}
Demo.