- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 C++ 模板实现混入,以支持基(模板化)类的一些“扩展”行为。
template< class Ch > Base {...};
template< class T > M1 : public T {...};
template< class T > M2 : public T {...};
// etc.
如果继承图中存在另一个 mixin,我需要修改一个 mixin 中的行为。 (理想情况下,这与先混合哪一个无关,但我愿意接受它们必须按特定顺序派生的限制。)
换句话说,如果我有:
typedef Base< char > base;
typedef M2< M1< base >> foo;
typedef M2< base > bar;
当混合使用 M1 时,M2 方法的行为需要更改 - M1 提供需要在计算 M2 结果时使用的数据成员。另一方面,如果不混入M2,则M2对那些完全无效的数据成员做了一定的保证。
那么我的问题是如何实现这个 C++ '03?
我怀疑有一种使用模板方法和特化的方法,但我的模板功能还不够强大。
根据 dauphic 的回复,我试过了,确实有效。完全复制 M2 模板的需要非常可怕,因此我接下来将尝试 SFINAE 方法。
#include <iostream>
#include <UnitTest++.h>
SUITE( Mixin_specialization_tests ) {
template< typename Ch > struct Base {
typedef Ch * pointer_type;
pointer_type p;
};
template< class T > struct M1 : public T {
typedef typename T::pointer_type pointer_type;
pointer_type m1p;
};
template< class T > struct M2 : public T {
typedef typename T::pointer_type pointer_type;
pointer_type m2p;
int compute( ) {
std::cout << "unspecialized compute()" << std::endl;
return 0;
}
};
template< >
template< class B > struct M2< M1<B> > : public M1<B> {
typedef typename M1< B >::pointer_type pointer_type;
pointer_type m2p;
int compute( ) {
std::cout << "specialized compute()" << std::endl;
int unused = M1< B >::m1p - m2p;
return 1;
}
};
typedef Base< char > Bch;
typedef M1< Bch > M1b;
typedef M2< Bch > M2b;
typedef M2< M1< Bch > > M2m1b;
TEST( unspecialized ) {
M2b m2b;
CHECK_EQUAL( 0, m2b.compute() );
}
TEST( specialized ) {
M2m1b m2m1b;
CHECK_EQUAL( 1, m2m1b.compute( ) );
}
}
经过一番努力,我设法让 SFINAE 变体也能正常工作。除此部分外,代码大部分相同:
template< class Query > struct is_M1 {
typedef char yes, (&no)[ 2 ];
static no has_m1_member(...);
template< class T, typename T::pointer_type T::* > struct dummy {};
template< class T >
static yes has_m1_member( T*, dummy<T, &T::m1p>* = 0 );
BOOST_STATIC_CONSTANT( bool, value =
( sizeof( has_m1_member( ( Query * )0 ) ) == sizeof( yes ) )
);
};
template< class T > struct M2 : public T {
typedef typename T::pointer_type pointer_type;
pointer_type m2p;
int compute( ) {
return compute_impl<T>( );
}
template< typename B >
typename boost::enable_if< is_M1< B >, int >::type compute_impl( ) {
std::cout << "sfinae: m1-compute" << std::endl;
return 1;
}
template< typename B >
typename boost::disable_if< is_M1<B>, int >::type compute_impl( ) {
std::cout << "sfinae: non-m1-compute" << std::endl;
return 0;
}
};
正如我在下面提到的,enable_if<> 模板不处理 bool 表达式(对我来说),所以我去了使用 disable_if<>,这似乎可以解决问题。
这些都不是特别令人高兴的事情,因为这段代码将被贡献给一个当前不使用 boost 的项目,并且因为复制整个模板的选项是维护的噩梦。但他们确实都完成了工作。我将考虑使用 boost 解决方案作为答案,只是因为希望 boost 代码成为标准,从而减少头痛。
谢谢大家
最佳答案
正如 dauphic 所建议的,当 M2 类的参数是 M1 时,您可以只对 M2 类进行部分特化,当然,您也可以对 M1 做同样的事情(通过简单的交错声明)。
但是,这会给您带来非常粗粒度的特化,即您将不得不重新定义 M2 的所有成员。如果只有少数成员需要不同,这可能会很烦人,也就是说,这个解决方案不能很好地扩展。不幸且恼人的是,C++ 不支持独立特化成员函数。
尽管如此,Sfinae 可以来帮助您,但这需要更多的工作。以下基本上只是一个经典的 Sfinae 方法,允许对类模板的各个成员函数进行特化:
#include <iostream>
#include <boost/utility/enable_if.hpp>
#include <boost/config.hpp>
template <class Model>
struct is_M1
{
typedef char yes;
typedef char (&no)[2];
template <class T, void (T::*)()>
struct dummy {};
template <class T>
static yes has_m1_function1(T*, dummy<T,&T::function1>* = 0);
static no has_m1_function1(...);
BOOST_STATIC_CONSTANT(
bool
, value = sizeof( has_m1_function1((Model*)0) ) == 1 );
};
template <typename T>
struct Base { T value; };
template <typename T>
struct M1 : public T {
void function1() { }; //I presume M1 would have at least one defining characteristic or member function
};
template <typename T>
struct M2 : public T {
void function2() { function2_impl<T>(); };
template <typename B>
typename boost::enable_if< is_M1<B>, void>::type function2_impl() {
std::cout << "M1 is mixed in T" << std::endl;
};
template <typename B>
typename boost::enable_if< !is_M1<B>, void>::type function2_impl() {
std::cout << "M1 is not mixed in T" << std::endl;
};
};
int main() {
M2< M1< Base<int> > > m2m1b;
M2< Base<int> > m2b;
m2b.function2();
m2m1b.function2();
};
关于c++ - 如何在两个 mixin 模板之间实现多态行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5518843/
背景: 我最近一直在使用 JPA,我为相当大的关系数据库项目生成持久层的轻松程度给我留下了深刻的印象。 我们公司使用大量非 SQL 数据库,特别是面向列的数据库。我对可能对这些数据库使用 JPA 有一
我已经在我的 maven pom 中添加了这些构建配置,因为我希望将 Apache Solr 依赖项与 Jar 捆绑在一起。否则我得到了 SolarServerException: ClassNotF
interface ITurtle { void Fight(); void EatPizza(); } interface ILeonardo : ITurtle {
我希望可用于 Java 的对象/关系映射 (ORM) 工具之一能够满足这些要求: 使用 JPA 或 native SQL 查询获取大量行并将其作为实体对象返回。 允许在行(实体)中进行迭代,并在对当前
好像没有,因为我有实现From for 的代码, 我可以转换 A到 B与 .into() , 但同样的事情不适用于 Vec .into()一个Vec . 要么我搞砸了阻止实现派生的事情,要么这不应该发
在 C# 中,如果 A 实现 IX 并且 B 继承自 A ,是否必然遵循 B 实现 IX?如果是,是因为 LSP 吗?之间有什么区别吗: 1. Interface IX; Class A : IX;
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在阅读标准haskell库的(^)的实现代码: (^) :: (Num a, Integral b) => a -> b -> a x0 ^ y0 | y0 a -> b ->a expo x0
我将把国际象棋游戏表示为 C++ 结构。我认为,最好的选择是树结构(因为在每个深度我们都有几个可能的移动)。 这是一个好的方法吗? struct TreeElement{ SomeMoveType
我正在为用户名数据库实现字符串匹配算法。我的方法采用现有的用户名数据库和用户想要的新用户名,然后检查用户名是否已被占用。如果采用该方法,则该方法应该返回带有数据库中未采用的数字的用户名。 例子: “贾
我正在尝试实现 Breadth-first search algorithm , 为了找到两个顶点之间的最短距离。我开发了一个 Queue 对象来保存和检索对象,并且我有一个二维数组来保存两个给定顶点
我目前正在 ika 中开发我的 Python 游戏,它使用 python 2.5 我决定为 AI 使用 A* 寻路。然而,我发现它对我的需要来说太慢了(3-4 个敌人可能会落后于游戏,但我想供应 4-
我正在寻找 Kademlia 的开源实现C/C++ 中的分布式哈希表。它必须是轻量级和跨平台的(win/linux/mac)。 它必须能够将信息发布到 DHT 并检索它。 最佳答案 OpenDHT是
我在一本书中读到这一行:-“当我们要求 C++ 实现运行程序时,它会通过调用此函数来实现。” 而且我想知道“C++ 实现”是什么意思或具体是什么。帮忙!? 最佳答案 “C++ 实现”是指编译器加上链接
我正在尝试使用分支定界的 C++ 实现这个背包问题。此网站上有一个 Java 版本:Implementing branch and bound for knapsack 我试图让我的 C++ 版本打印
在很多情况下,我需要在 C# 中访问合适的哈希算法,从重写 GetHashCode 到对数据执行快速比较/查找。 我发现 FNV 哈希是一种非常简单/好/快速的哈希算法。但是,我从未见过 C# 实现的
目录 LRU缓存替换策略 核心思想 不适用场景 算法基本实现 算法优化
1. 绪论 在前面文章中提到 空间直角坐标系相互转换 ,测绘坐标转换时,一般涉及到的情况是:两个直角坐标系的小角度转换。这个就是我们经常在测绘数据处理中,WGS-84坐标系、54北京坐标系
在软件开发过程中,有时候我们需要定时地检查数据库中的数据,并在发现新增数据时触发一个动作。为了实现这个需求,我们在 .Net 7 下进行一次简单的演示. PeriodicTimer .
二分查找 二分查找算法,说白了就是在有序的数组里面给予一个存在数组里面的值key,然后将其先和数组中间的比较,如果key大于中间值,进行下一次mid后面的比较,直到找到相等的,就可以得到它的位置。
我是一名优秀的程序员,十分优秀!