std::tuple_element 和参考资料
std::tuple_element and references
我学习std::tuple.
让我们有:
struct test_struct{};
我写
std::cout << typeid(std::tuple_element_t<0, std::tuple<struct test_struct &>>).name();
我期待的是类型
struct test_struct &
但是我收到了:
struct test_struct
如何提取类型 struct test_struct &(最好使用 std11)?
谢谢。
由于 typeid
运算符对引用类型 不起作用 ,
§5.2.8/4-5
If type is a reference type, the result refers to a std::type_info
object representing the referenced type.
In all cases, cv-qualifiers are ignored by typeid (that is, typeid(T)==typeid(const T)
)
您可以编写一些包装器来找出引用类型或带有 cv 限定符的类型的名称。
template<typename T>
struct typeid_hlp
{
static std::string name() { return typeid(T).name(); }
};
template<typename T>
struct typeid_hlp<T&>
{
static std::string name() { return typeid_hlp<T>::name() + std::string(" &"); }
};
template<typename T>
struct typeid_hlp<T&&>
{
static std::string name() { return typeid_hlp<T>::name() + std::string(" &&"); }
};
template<typename T>
struct typeid_hlp<const T>
{
static std::string name() { return std::string("const ") + typeid_hlp<T>::name(); }
};
template<typename T>
struct typeid_hlp<volatile T>
{
static std::string name() { return std::string("volatile ") + typeid_hlp<T>::name(); }
};
并像
一样使用它
int main()
{
std::cout << typeid_hlp<int>::name() << std::endl; // int
std::cout << typeid_hlp<int&>::name() << std::endl; // int &
std::cout << typeid_hlp<const int>::name() << std::endl; // const int
std::cout << typeid_hlp<volatile const int * const &>::name() << std::endl; // const int const volatile * __ptr64 &
}
我学习std::tuple.
让我们有:
struct test_struct{};
我写
std::cout << typeid(std::tuple_element_t<0, std::tuple<struct test_struct &>>).name();
我期待的是类型
struct test_struct &
但是我收到了:
struct test_struct
如何提取类型 struct test_struct &(最好使用 std11)?
谢谢。
由于 typeid
运算符对引用类型 不起作用 ,
§5.2.8/4-5
If type is a reference type, the result refers to astd::type_info
object representing the referenced type.In all cases, cv-qualifiers are ignored by typeid (that is,
typeid(T)==typeid(const T)
)
您可以编写一些包装器来找出引用类型或带有 cv 限定符的类型的名称。
template<typename T>
struct typeid_hlp
{
static std::string name() { return typeid(T).name(); }
};
template<typename T>
struct typeid_hlp<T&>
{
static std::string name() { return typeid_hlp<T>::name() + std::string(" &"); }
};
template<typename T>
struct typeid_hlp<T&&>
{
static std::string name() { return typeid_hlp<T>::name() + std::string(" &&"); }
};
template<typename T>
struct typeid_hlp<const T>
{
static std::string name() { return std::string("const ") + typeid_hlp<T>::name(); }
};
template<typename T>
struct typeid_hlp<volatile T>
{
static std::string name() { return std::string("volatile ") + typeid_hlp<T>::name(); }
};
并像
一样使用它int main()
{
std::cout << typeid_hlp<int>::name() << std::endl; // int
std::cout << typeid_hlp<int&>::name() << std::endl; // int &
std::cout << typeid_hlp<const int>::name() << std::endl; // const int
std::cout << typeid_hlp<volatile const int * const &>::name() << std::endl; // const int const volatile * __ptr64 &
}