#include <iostream>
#include <tuple>
struct my_struct {
const std::tuple<int, float> t{5, 3.14};
template<std::size_t i>
decltype(std::get<i>(t)) get() {
return std::get<i>(t);
}
};
namespace std {
template<>
struct tuple_size<my_struct> :
integral_constant<
std::size_t,
std::tuple_size_v<decltype(std::declval<my_struct>().t)>> {
};
template<std::size_t i>
struct tuple_element<i, my_struct> {
using type = tuple_element_t<
i,
decltype(std::declval<my_struct>().t)>;
};
}
int main() {
my_struct s;
auto [x, y] = s;
std::cout << (std::is_const_v<decltype(x)> ? "x is const" : "x is not const")
<< '\n';
auto [z, w] = s.t;
std::cout << (std::is_const_v<decltype(z)> ? "z is const" : "z is not const")
<< '\n';
return 0;
}
This code prints
x is const
z is not const
Why is x const but z is not if I specialized std::tuple_element using the type of the struct t directly and defined get using the type returned by std::get<i>(t)?
How can I fix this problem (where fixing it means making my custom type behave exactly like the struct it contains when using structured bindings)?
constfrom the member definition?