has_arrow_operator type trait 模板

hellwolf hellwolf 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
From: http://www.cnscn.org/htm_data/179/0805/14920.html


#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/type_traits.hpp>


/* type trait : has_arrow_operator<T> */
template <typename T>
struct has_arrow_operator {
template <int> struct test;

template<typename U>
static boost::type_traits::yes_type check_sig(test<sizeof(&U::operator ->)>* = NULL);

template<typename U>
static boost::type_traits::no_type check_sig(...);

static const bool value = sizeof(check_sig<T>(0)) == sizeof(boost::type_traits::yes_type);
};

/* operator -> available */
class A {
public:
A* operator -> ();
};

/* operator -> hidden */
class B {
protected:
B* operator -> ();
};

/* operator -> not available */
class C {
};

int main() {
std::cout << has_arrow_operator<int>::value << std::endl;
std::cout << has_arrow_operator<boost::shared_ptr<int> >::value << std::endl;
std::cout << has_arrow_operator<A>::value << std::endl;
// ????? how to make this false
//std::cout << has_arrow_operator<B>::value << std::endl;
std::cout << has_arrow_operator<C>::value << std::endl;
}


执行结果:
$ dync++ has_arrow_operator.cpp
0
1
1
0

不过对 B 使用的时候,编译器报错了, 怎么解决呢?

编译器: gcc (GCC) 4.4.1 20090725 (Red Hat 4.4.1-2)
...全文

相关推荐

free-arrow:在Scala中实现Free Arrow的实现以及其他使用Arrows的有用工具

自由箭 在Scala中实现Free Arrow的实现以及其他使用Arrows的有用工具基于将的论文使用“自由箭头”构建一个计算图,该图可以解释为不同的执行上下文,并可以重用以从较小的上下文创建更复杂的

yoreua

Rust_Study

**trait与泛型**:trait定义了一组方法签名,实现了trait的类型可以互相操作。泛型允许编写通用代码,减少代码重复。3.

火君

CISP-CSE注册云安全工程师备考资源包基础教程

随着云计算技术的普及,云安全已成为网络安全领域的核心议题。CISP-CSE(注册云安全工程师)认证由中国信息安全测评中心颁发,旨在培养具备云安全专业知识和实践能力的实战型人才。本教程将结合"CISP-CSE注册云安全工程师备考资源包",系统性地为您梳理云安全的核心知识体系,并提供实战案例分析,助您高效备考。

t0_54coder

绘图模拟:在冲动环境中固定时间的示例 附matlab代码.rar

绘图模拟:在冲动环境中固定时间的示例 附matlab代码.rar

天天Matlab科研工作室

基于规则的电动汽车混合能源管理系统,采用MATLAB、Simulink、Simscape Electrical和Stateflow。.zip

1.版本:matlab2014a/2019b/2024b 2.附赠案例数据可直接运行。 3.代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。 4.适用对象:计算机,电子信息工程、数学等专业的大学生课程设计、期末大作业和毕业设计。

海神之光
评论 15
收藏 已收藏
分享
返回顶部
hellwolf
C++ 语言 65,214 成员 250,514 内容