gpt4 book ai didi

c++ - "same"模板参数在不同上下文中的 trait typedef 变化

转载 作者:行者123 更新时间:2023-11-30 03:57:27 25 4
gpt4 key购买 nike

对于大量来源感到抱歉 - 我将其缩小到我认为对问题描述合理的范围。

我尝试(递归地)提取任何容器的“内容类型”(分别使用特定特征或 sfinae 特征进行识别)。

我要实现的是content_type<vector<A<int>> == int [如果has_contentcontent_traitA有内容并定义内容类型]。

以下程序发出 (MSVC)

content_type<struct A<double> (3627049818)> = double(2699759368)
content_type<struct A<double> (3627049818)> = struct A<double>(3627049818)
0 1 1

这基本上意味着我的特点是在不同上下文中为相同类型提供“不同”(但相同?!)的内容类型。

(注意:将 std::vector 标识为包含内容的类型是通过对原始程序中的 begin/end 或 subscript[]/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>::value0其中 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.

当您将 TT& 进行比较时,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>

关于c++ - "same"模板参数在不同上下文中的 trait typedef 变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27966144/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com