C ++:模板参数循环依赖性 [英] C++: Template Parameter Cyclic Dependency

查看:151
本文介绍了C ++:模板参数循环依赖性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个比一个语言问题本身更好的最佳实践问题,因为我已经有一个工作的解决方案,似乎是一个常见的绊脚石在C + +。



我正在处理模板参数替换中的典型循环依赖问题。我有以下两个类:

 模板< class X> 
class A {/ * ... * /};

template< class X>
B类{/ * ... * /};

,我想将每个实例化如下:

  //伪代码 - 无效的C ++。 
A< B>一个;
B< A> b;

这就是说,我想将A绑定到B,B绑定到A。 >

我可以通过继承技巧的前进声明来解决这个问题:

  class sA; 
class sB;

class sA:public A< sB> {/ * ... * /};
class sB:public B< sA> {/ * ... * /};

但这会带来一系列问题,因为 sA 不是 A B 。例如,我不能调用 A 的构造函数,而没有将它们正确复制到 sA ,或者以某种方式闪烁代码。



我的问题是:处理这个问题最好的实际方法是什么?任何特别聪明的解决方案这个问题?



我使用MSVC2008和G ++,但解决方案与编译器特定的扩展是受欢迎的。



感谢,



Alek

解决方案

一个模板的类型命名所有的参数,你不能有一个无尽的参数化循环。



你可能(当然)只是试图发送信息在相反的方向在同时。没有问题,但你不能封装在提供实现的类中的信息。

  template<类W> //定义一个抽象类来传递数据
struct widget_traits {};

模板<>
struct widget_traits< SpringySpring> {//专门把数据放在它
struct properties {...};
enum {boing = 3};
};

template<类别V>
struct veeblfetzer_traits {};

模板<>
struct veeblfetzer_traits< VonNumum> {
typedef int potrzebie;
};

template< struct WT,struct VT> //通过使用作为参数传递信息
struct MyWidget {...};

template< struct VT,struct WT> // both ways
struct MyVeeblfetzer {...};


This is more a best practice question than a language question in itself, since I already have a working solution to what seems to be a common stumbling block in C++.

I'm dealing with a typical cyclic dependency issue in template parameter substitutions. I have the following pair of classes:

template<class X>
class A { /* ... */ };

template<class X>
class B { /* ... */ };

and I want to instantiate each one as the following:

// Pseudocode -- not valid C++.
A<B> a;
B<A> b;

that is, I want to 'bind' A to B, and B to A.

I can solve the problem, in a gross way, through a forward declaration with inheritance trick:

class sA;
class sB;

class sA : public A<sB> { /* ... */ };
class sB : public B<sA> { /* ... */ };

but this brings in a set of problems, since sA and sB are not indeed A and B. For example, I cannot invoke A's constructors without properly duplicating them into sA, or somehow sparkling casts around the code.

My question is: what is the best practical way to deal with this issue? Any specially clever solution to this problem?

I am using both MSVC2008 and G++, but solutions with compiler-specific extensions are welcome.

Thanks,

Alek

解决方案

Since a template's type names all its parameters, you can't have an endless loop of parameterization.

You are probably (certainly) just trying to send information in opposite directions at the same time. There's no problem with that, but you can't encapsulate the information in the classes that provide implementation.

template< class W > // define an abstract class to pass data
struct widget_traits {};

template<>
struct widget_traits< SpringySpring > { // specialize to put data in it
    struct properties { … };
    enum { boing = 3 };
};

template< class V >
struct veeblfetzer_traits {};

template<>
struct veeblfetzer_traits< VonNeumann > {
    typedef int potrzebie;
};

template< struct WT, struct VT > // pass info by using as argument
struct MyWidget { … };

template< struct VT, struct WT > // both ways
struct MyVeeblfetzer { … };

这篇关于C ++:模板参数循环依赖性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆