- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于大量来源感到抱歉 - 我将其缩小到我认为对问题描述合理的范围。
我尝试(递归地)提取任何容器的“内容类型”(分别使用特定特征或 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
这基本上意味着我的特点是在不同上下文中为相同类型提供“不同”(但相同?!)的内容类型。
(注意:将 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>::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>
关于c++ - "same"模板参数在不同上下文中的 trait typedef 变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27966144/
我正在尝试在具有相同特征的盒装特征对象上实现特征。我以前对其方法采用的特征做过这样的事情&Self,它工作得很好,但不是Self。。我意识到在这个特定的例子中,我可以将fn get_*()函数更改为返
在编写带有特征的代码时,您可以将特征放在特征边界中: use std::fmt::Debug; fn myfunction1(v: Box) { println!("{:?}", v); }
我有一个特征正在使用另一个特征,现在我收到关于类中不存在的函数的错误。我简化了代码: 设置.php: getMessage());} } } ?> 数据库.php pdo=new PDO("m
这个问题在这里已经有了答案: Why doesn't Rust support trait object upcasting? (4 个回答) 2年前关闭。 如果我有 Box , 我可以返回 &dyn
给定这段代码: trait Trait {} struct Child; impl Trait for Child {} struct Father { child: &'a Box, } i
这个问题的标题与许多相关问题非常相似,但我还没有找到一个讨论这个特定问题的问题。 我有一个 Color非常简单定义的类型,如下所示: pub struct Color { red: u8,
我有三个特点。 Trait Param、GroupId 和 SessionId。特征 GroupId 和 SessionId 包括 Param。 类 GroupSession 包括特征 GroupId
查看 Traversable 和 TraversableLike 的 scaladoc,我很难弄清楚它们之间的区别是什么(除了一个扩展另一个)。文档中唯一明显的区别是它说 Traversable 是一
我有以下代码: trait T { type AT; fn foo(&self); } struct AbstractT { t: Box>, } impl T for Abs
如何在 Rust 中尝试类似以下的操作? builder 类是一个 trait 对象,它返回另一个 trait 对象(类型删除),其中选择的实现由我们正在使用的 builder trait 的特定对象
我想创建一个新向量,其中包含实现 Trait 的对象,来 self 已有的包含这些对象的一些向量。 trait Foo { // } struct Bar { i: i32, } st
到目前为止,在我的项目中,我使用了许多特征来允许在单元测试中模拟/ stub 以注入(inject)依赖项。然而,到目前为止我正在做的事情的一个细节似乎非常可疑,以至于我很惊讶它甚至可以编译。我担心正
假设我有一些特质: trait MyTrait { fn function1(&self); } 和一些实现它的类型: struct MyStruct { number: i32, }
更新:不止我一个人在思考这个问题,看来这确实是一个错误。参见 here .修复的那一天将是美好的一天! :) 这开始为 I love PHP traits! I'm going to use them
以下特征Parser[+T]是扩展接受 Input 的函数的特征并返回 Result[T] . trait Parser[+T] extends (Input => Result[T]) 那是对的吗
我有一个类型,我可以通过它访问它的方法 SomeTrait::::method() 但我不明白那和之间的区别 >::method() 在 C++ 中,我希望这样: SomeTrait::method(
在下面的代码中,不可能从对实现相同特征的动态大小类型的引用中获得对特征对象的引用。为什么会这样呢?如果我可以同时调用trait方法,那么&dyn Trait和&(?Sized + Trait)之间到底
我是 Rust 的新手,我想通过实现一些小项目来学习这门语言并更好地理解。我的第一次尝试是解析从 MQTT 代理收到的 JSON 数据。 我很高兴在 tornado 的帮助下轻松完成这项工作。和 se
在下面的代码中,不可能从对实现相同特征的动态大小类型的引用中获得对特征对象的引用。为什么会这样呢?如果我可以同时调用trait方法,那么&dyn Trait和&(?Sized + Trait)之间到底
这个问题在这里已经有了答案: Why is the `Sized` bound necessary in this trait? (2 个回答) 1年前关闭。 我有一个特质 Vertex我想要几个结构
我是一名优秀的程序员,十分优秀!