"same" 模板参数的 trait typedef 在不同上下文中的变化
Trait typedef changes in different contexts for the "same" template parameter
对于大量的来源感到抱歉 - 我缩小了它的范围,因为我认为对问题的描述是合理的。
我尝试(递归地)提取任何容器的 "content type"(分别使用特定特征或 sfinae 特征进行识别)。
我要实现的是content_type<vector<A<int>> == int
[如果has_content
和content_trait
说A
有内容,定义内容类型。
以下程序发出 (MSVC)
content_type<struct A<double> (3627049818)> = double(2699759368)
content_type<struct A<double> (3627049818)> = struct A<double>(3627049818)
0 1 1
这基本上意味着我的特征是在不同上下文中为同一类型提供 "different"(但相同?!)内容类型。
(注意:std::vector
作为内容包含类型的标识是通过对begin/end或原始程序中的下标[]/size()的特征检查传递的。)
#include <iostream>
#include <type_traits>
#include <vector>
#include <typeinfo>
#include <cstddef>
template<class T> struct content_trait;
template<class T> struct has_content : std::false_type {};
template<class T> struct has_content<std::vector<T>> : std::true_type{};
namespace detail
{
template<class T, bool has_content = content_trait<T>::value>
struct content_helper { typedef T type; };
template<class T>
struct content_helper<T, true>
{ typedef typename content_trait<T>::type type; };
template<class T>
struct content_vec_helper
{
typedef decltype(*(std::declval<T&>().begin())) value_type;
typedef typename content_helper<value_type,
has_content<value_type>::value>::type type;
};
}
template <class T> struct content_trait
{ typedef typename detail::content_vec_helper<T>::type type; };
template<class T> using content_type
= typename detail::content_helper<T, has_content<T>::value>::type;
template<class T> struct A {};
template<class T> struct content_trait<A<T>> { typedef content_type<T> type; };
template<class T> struct has_content<A<T>> : std::true_type { };
template<class T> char const * nameof() { return typeid(T).name(); }
template<class T> std::size_t hashof() { return typeid(T).hash_code(); }
template<class T>
void check()
{
std::cout << "content_type<" << nameof<T>() << " (" << hashof<T>() << ")";
std::cout << "> = " << nameof <content_type<T>>();
std::cout << "(" << hashof<content_type<T>>() << ")";
std::cout << std::endl;
}
template<class T, class U>
void same()
{
std::cout << std::is_same<T, U>::value << " ";
std::cout << (hashof<T>() == hashof<U>()) << " ";
std::cout << (typeid(T) == typeid(U)) << std::endl;
}
int main()
{
typedef A<double> a_type;
typedef detail::content_vec_helper<std::vector<A<double>>>::value_type b_type;
check<a_type>();
check<b_type>();
same<a_type, b_type>();
#ifdef _MSC_VER
system("pause");
#endif
return 0;
}
所以...
为什么 is_same<A,B>::value
说 0
而 typeid(A) == typeid(B) && typeid(A).hash_code() == typeid(B).hash_code()
?
为什么不是 content_type<std::vector<A<double>>> == double
?
目前,您有
detail::content_vec_helper<std::vector<A<double>>>::value_type == A<double>&
来自http://en.cppreference.com/w/cpp/language/typeid
1) [..]. If type is a reference type, the result refers to the referenced type.
将T
与T&
进行比较,typeid
比较相同,但类型不同。
您可以将 content_vec_helper
更改为:
template <class T> struct content_vec_helper
{
typedef typename std::decay<decltype(*(std::declval<T&>().begin()))>::type value_type;
typedef typename content_helper<value_type, has_content<value_type>::value>::type type;
};
有
detail::content_vec_helper<std::vector<A<double>>>::value_type == A<double>
对于大量的来源感到抱歉 - 我缩小了它的范围,因为我认为对问题的描述是合理的。
我尝试(递归地)提取任何容器的 "content type"(分别使用特定特征或 sfinae 特征进行识别)。
我要实现的是content_type<vector<A<int>> == int
[如果has_content
和content_trait
说A
有内容,定义内容类型。
以下程序发出 (MSVC)
content_type<struct A<double> (3627049818)> = double(2699759368)
content_type<struct A<double> (3627049818)> = struct A<double>(3627049818)
0 1 1
这基本上意味着我的特征是在不同上下文中为同一类型提供 "different"(但相同?!)内容类型。
(注意:std::vector
作为内容包含类型的标识是通过对begin/end或原始程序中的下标[]/size()的特征检查传递的。)
#include <iostream>
#include <type_traits>
#include <vector>
#include <typeinfo>
#include <cstddef>
template<class T> struct content_trait;
template<class T> struct has_content : std::false_type {};
template<class T> struct has_content<std::vector<T>> : std::true_type{};
namespace detail
{
template<class T, bool has_content = content_trait<T>::value>
struct content_helper { typedef T type; };
template<class T>
struct content_helper<T, true>
{ typedef typename content_trait<T>::type type; };
template<class T>
struct content_vec_helper
{
typedef decltype(*(std::declval<T&>().begin())) value_type;
typedef typename content_helper<value_type,
has_content<value_type>::value>::type type;
};
}
template <class T> struct content_trait
{ typedef typename detail::content_vec_helper<T>::type type; };
template<class T> using content_type
= typename detail::content_helper<T, has_content<T>::value>::type;
template<class T> struct A {};
template<class T> struct content_trait<A<T>> { typedef content_type<T> type; };
template<class T> struct has_content<A<T>> : std::true_type { };
template<class T> char const * nameof() { return typeid(T).name(); }
template<class T> std::size_t hashof() { return typeid(T).hash_code(); }
template<class T>
void check()
{
std::cout << "content_type<" << nameof<T>() << " (" << hashof<T>() << ")";
std::cout << "> = " << nameof <content_type<T>>();
std::cout << "(" << hashof<content_type<T>>() << ")";
std::cout << std::endl;
}
template<class T, class U>
void same()
{
std::cout << std::is_same<T, U>::value << " ";
std::cout << (hashof<T>() == hashof<U>()) << " ";
std::cout << (typeid(T) == typeid(U)) << std::endl;
}
int main()
{
typedef A<double> a_type;
typedef detail::content_vec_helper<std::vector<A<double>>>::value_type b_type;
check<a_type>();
check<b_type>();
same<a_type, b_type>();
#ifdef _MSC_VER
system("pause");
#endif
return 0;
}
所以...
为什么 is_same<A,B>::value
说 0
而 typeid(A) == typeid(B) && typeid(A).hash_code() == typeid(B).hash_code()
?
为什么不是 content_type<std::vector<A<double>>> == double
?
目前,您有
detail::content_vec_helper<std::vector<A<double>>>::value_type == A<double>&
来自http://en.cppreference.com/w/cpp/language/typeid
1) [..]. If type is a reference type, the result refers to the referenced type.
将T
与T&
进行比较,typeid
比较相同,但类型不同。
您可以将 content_vec_helper
更改为:
template <class T> struct content_vec_helper
{
typedef typename std::decay<decltype(*(std::declval<T&>().begin()))>::type value_type;
typedef typename content_helper<value_type, has_content<value_type>::value>::type type;
};
有
detail::content_vec_helper<std::vector<A<double>>>::value_type == A<double>