To implement a custom tuple, I have written the following declarations to access an element:
1 2 3 4 5 6 7
|
template<int N, /// non-const version
typename... T>
Select<N, T...>& Estd::get(Estd::tuple<T...>& t);
template<int N, /// const version
typename... T>
Select<N, T...>& Estd::get(const Estd::tuple<T...>& t);
|
Note: The Select<N, T...>& function selects the Nth type of the T... template parameter pack.
I have no problem accessing a required element of a non-const tuple. Eg:
|
cout << Estd::get<3> (t3); /// t3 is a non-const tuple
|
However, when I try accessing an element of a const tuple, I get an error:
|
cout << Estd::get<3> (ct3); /// ct3 is a const tuple
|
The error is:
No matching function for call to 'get(const Estd::tuple<T...>& t)'
template argument deduction/substitution failed
types Estd::tuple<T ...> and const Estd::tuple<int> have incompatible cv-qualifiers
|
I have analyzed this error and I think the following is what is happening:
When function Estd::get() is invoked with a const tuple<T...>, only the 1st template argument N is specified (3). Therefore the 2nd template arg is deduced as T... (without the const qualifier), since it is the passed tuple which is const, not its encapsulated types T... .
Therefore, the overload of the get() function that takes a non-const tuple is instantiated rather than the overload that takes a const tuple.
I think this is why the problem occurs.
If so, how can I devise a solution?
Thanks.