C++:std::tuple_size/tuple_element 可以专门化吗?
C++: Can std::tuple_size/tuple_element be specialized?
std::tuple_size
和 std::tuple_element
的特化是否允许用于自定义类型?
我想是的,但我想绝对确定,我找不到任何具体信息。
示例(省略命名空间、成员函数和 get<I>
重载):
template <typename T, size_t N>
struct vector { T _data[N]; };
template<size_t I, typename T, size_t N>
constexpr T& get(vector<T,N>& vec) { return vec._data[I]; }
namespace std {
template<typename T, size_t N>
class tuple_size< vector<T,N> > : public std::integral_constant<size_t, N> { };
template<size_t I, typename T, size_t N>
class tuple_element< I, vector<T,N> > { public: using type = T; };
}
我需要它用于结构化绑定:
void f(vector<T,3> const& vec)
{
auto& [x,y,z] = vec;
// stuff...
}
用户定义类型的特化通常很好,而且一直都是。 N4606, [namespace.std]/1:
A program may add a template specialization for any standard library template to namespace std
only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.
对于tuple_size
,原始模板的要求在[tuple.helper]/1中指定:
All specializations of tuple_size<T>
shall meet the UnaryTypeTrait
requirements with a BaseCharacteristic
of integral_constant<size_t, N>
for some N
.
UnaryTypeTrait
,反过来,在[meta.rqmts]/1:
A UnaryTypeTrait describes a property of a type. It shall be a class template that takes one template type argument and, optionally, additional arguments that help define the property being described. It shall be DefaultConstructible
, CopyConstructible
, and publicly and unambiguously derived, directly or indirectly, from its BaseCharacteristic, which is a specialization of the template integral_constant
, with the arguments to the template integral_constant
determined by the requirements for the particular property being described. The member names of the BaseCharacteristic shall not be hidden and shall be unambiguously available in the UnaryTypeTrait.
tuple_element
的要求在 [tuple.helper]/6 和 [meta.rqmts]/3 中指定,但为了简洁起见,我不会 [=39= 】 他们在这里。可以说专攻确实是合法的...
std::tuple_size
和 std::tuple_element
的特化是否允许用于自定义类型?
我想是的,但我想绝对确定,我找不到任何具体信息。
示例(省略命名空间、成员函数和 get<I>
重载):
template <typename T, size_t N>
struct vector { T _data[N]; };
template<size_t I, typename T, size_t N>
constexpr T& get(vector<T,N>& vec) { return vec._data[I]; }
namespace std {
template<typename T, size_t N>
class tuple_size< vector<T,N> > : public std::integral_constant<size_t, N> { };
template<size_t I, typename T, size_t N>
class tuple_element< I, vector<T,N> > { public: using type = T; };
}
我需要它用于结构化绑定:
void f(vector<T,3> const& vec)
{
auto& [x,y,z] = vec;
// stuff...
}
用户定义类型的特化通常很好,而且一直都是。 N4606, [namespace.std]/1:
A program may add a template specialization for any standard library template to namespace
std
only if the declaration depends on a user-defined type and the specialization meets the standard library requirements for the original template and is not explicitly prohibited.
对于tuple_size
,原始模板的要求在[tuple.helper]/1中指定:
All specializations of
tuple_size<T>
shall meet theUnaryTypeTrait
requirements with aBaseCharacteristic
ofintegral_constant<size_t, N>
for someN
.
UnaryTypeTrait
,反过来,在[meta.rqmts]/1:
A UnaryTypeTrait describes a property of a type. It shall be a class template that takes one template type argument and, optionally, additional arguments that help define the property being described. It shall be
DefaultConstructible
,CopyConstructible
, and publicly and unambiguously derived, directly or indirectly, from its BaseCharacteristic, which is a specialization of the templateintegral_constant
, with the arguments to the templateintegral_constant
determined by the requirements for the particular property being described. The member names of the BaseCharacteristic shall not be hidden and shall be unambiguously available in the UnaryTypeTrait.
tuple_element
的要求在 [tuple.helper]/6 和 [meta.rqmts]/3 中指定,但为了简洁起见,我不会 [=39= 】 他们在这里。可以说专攻确实是合法的...