结构化绑定功能表示,如果tuple_size
模板是完整类型,它会与元组类似的分解一起使用。当std::tuple_size
给定类型在程序中的某一点是完整类型而在另一点不完整时会发生什么?
#include <iostream>
#include <tuple>
using std::cout;
using std::endl;
class Something {
public:
template <std::size_t Index>
auto get() {
cout << "Using member get" << endl;
return std::get<Index>(this->a);
}
std::tuple<int> a{1};
};
namespace {
auto something = Something{};
}
void foo() {
auto& [one] = something;
std::get<0>(one)++;
cout << std::get<0>(one) << endl;
}
namespace std {
template <>
class tuple_size<Something> : public std::integral_constant<std::size_t, 1> {};
template <>
class tuple_element<0, Something> {
public:
using type = int;
};
}
int main() {
foo();
auto& [one] = something;
cout << one << endl;
}
(此处转载https://wandbox.org/permlink/4xJUEpTAyUxrizyU)
在上面的程序中,类型在程序Something
中的一个点通过公共数据成员进行分解,并在另一个点像分解一样回退到元组。我们std::tuple_size
是否在幕后使用隐含的“已完成”检查违反了 ODR?