【问题标题】:How can I define operations on a template without caring what type it's instantiated with?如何在模板上定义操作而不关心它实例化的类型?
【发布时间】:2009-10-12 12:26:07
【问题描述】:

我继承了一堆定义了许多数据包类型的网络代码。我必须编写一堆转换函数,它们采用某种类型的结构,并将值复制到具有相同字段但顺序不同的其他结构中(作为复杂的部分平台位顺序转换的一部分——不要'任务)。另外,我知道下面可能有更好的方法来表达转换等,但我目前并不关心这些。特别是,我不能让 convert 通过引用获取它的输出变量,因为我将传递位域,这会产生编译器错误。

所以有一堆这样的结构:

struct foo {
   int bar;
   int baz;
};
struct foo_x86 {
   int baz;
   int bar;
};

还有一堆这样的函数,可以在两者之间进行转换:

foo_x86 转换(const foo& in){ foo_x86 输出; out.bar = in.bar; out.baz = in.baz; 退出; }

这没什么大不了的。我遇到的问题是:还有一个模板结构看起来像这样:

template <class T>
struct Packet {
   HeaderType head;
   T          data;
};

这个模板有很多实例化,使用上面的包类型作为模板参数,例如:

struct superfoo {
   Packet<foo> quux;
};
struct superfoo_x86 {
   Packet<foo_x86> quux;
};

现在,假设存在一个函数 foo_x86 转换(常量 foo&); 有没有办法创建一个模板函数来处理调用这个转换函数的 Packet 对象?

例如,我想要一些看起来像这样的东西:

template <class type_1, class type_2>
Packet<type_2> convert(const Packet<type_1>& in) {
   Packet<type_2> out;
   out.head = in.head;
   out.data = convert(in.data);
   return out;
}

这将在如下函数中起作用:

superfoo_x86 convert(const superfoo& in) {
   superfoo_x86 out;
   out.quux = convert(in.quux);
   return out;
}

我希望能够转换 Packet 对象而不关心它们被实例化的类型,并且我希望避免必须为每个可能的 Packet 实例声明单独的转换函数。

C++ 中的模板是否与模板有关?

【问题讨论】:

  • 你应该在你的包模板类中声明这个模板函数,所以你不会关心类型;)BR
  • +1 用于使用如此多的元句法变量! ;-) 不过,认真阅读您的样本,这看起来不错。该模板应该做你想做的事。你遇到什么问题?编译器错误?
  • 特别是,编译器说“错误:没有匹配函数调用 `convert(const Packet&)',因为它可能无法识别(自动?)实例化这个具有 类型的模板函数可以解决问题。
  • 由于没有函数参数涉及type_2,因此编译器无法推断其类型值并自动将convert解析为模板函数。你可以明确地做到这一点:convert&lt;foo, foo_x86&gt;(in.quux)。一个缺陷是这将 super* 类型和它们的 quux.data 成员的类型解耦。要修复该缺陷,请将 typedef 添加到 super* 类,使调用变为 convert&lt;typename superfoo::data_type, typename superfoo_x86::data_type &gt;(in.quux),或使用特征模板类:convert&lt;typename traits&lt;superfoo&gt;::data_type, typename traits&lt;superfoo_x86&gt;::data_type &gt;(in.quux)。

标签: c++ templates


【解决方案1】:

我不确定我是否正确理解了您的问题。

我建议您将最后一个“转换”函数修改为:

template<typename Type1, typename Type2>
void convert(const Type1& in, Type2& out) 
{
   convert(in.quux, out.quux);
}

还有之前的:

template <class type_1, class type_2>
void convert(const Packet<type_1>& in, Packet<type_2>& out) 
{
   out.head = in.head;
   convert(in.data, out.data);
}

最后,定义基本类型的特化。

template<>
void convert( const int& in, int& out )
{
  out = in;
}

待测试,但应该可以正常工作。

【讨论】:

  • 我应该澄清一下,我的转换函数不能通过引用传递 out 参数,因为有些字段是位字段。
【解决方案2】:

我最终做了一些类似于 outis 在对原始问题的评论中建议的事情。首先,我创建了一个非专业的转换类模板,如下所示:

template <class type_1>
struct conversion {};

然后我输入了一个宏来专门处理这个类(我相信这就像建议的特征模板,对吧?),就像这样:

#define GEN_CON(type_1, type_2) \
    template <> struct conversion<type_1> { typedef type_2 type; }

然后,对于从 foo 到 foo_x86 的每个转换函数,我在转换函数定义之后的标头中添加了以下宏调用::

foo_x86 convert (const foo&);
GEN_CON(foo, foo_x86);

这会为 foo 和 foo_x86 实例化一个转换模板,如下所示:

template <>
struct conversion<foo> { typedef foo_x86 type; }

现在,我可以参考conversion::type 来获取foo_x86。
所以,最后,我可以创建我的数据包转换函数,引用转换类,如下所示:

template <class type_1>
Packet< typename conversion<type_1>::type > convert(const Packet<type_1>& in) {
   Packet< typename conversion<type_1>::type > out;
   out.head = convert(in.head);
   out.data = convert(in.data);
   return out;
}

这允许编译器很好地解析对 Packet convert(const Packet& in) 的引用,而无需将模板参数传递给调用(如 da_m_n 建议的那样)。

感谢您的所有建议!

【讨论】:

    【解决方案3】:

    这样调用转换怎么样?

    out.data = convert<type_1, type_2>(in.data);   
    

    【讨论】:

    • 此转换有效,但这意味着我的自动生成脚本需要模板转换功能的特殊情况。我希望避免这种情况。
    【解决方案4】:

    出于好奇,转换构造函数会有帮助吗?

    struct foo_x86;
    struct superfoo_x86;
    
    struct foo {
       int bar;
       int baz;
       explicit foo(const foo_x86& from);
    };
    struct foo_x86 {
       int baz;
       int bar;
       explicit foo_x86(const foo& from) : bar(from.bar), baz(from.baz) {}
    };
    foo::foo(const struct foo_x86& from) : bar(from.bar), baz(from.baz) {}
    
    template <class T>
    struct Packet {
       HeaderType head;
       T          data;
       template <class S>
         explicit Packet(const Packet<S>& from) : head(from.head), data(from.data) {}
    };
    struct superfoo {
       Packet<foo> quux;
       explicit superfoo(const superfoo_x86& from);
    };
    struct superfoo_x86 {
       Packet<foo_x86> quux;
       explicit superfoo_x86(const superfoo& from) : quux(from.quux) {}
    };
    superfoo::superfoo(const superfoo_x86& from) : quux(from.quux) {}
    

    似乎他们会通过自动推导模板参数来解决问题。跳过 explicit 说明符可能对您有意义。

    【讨论】:

    • 这是个好主意,我没有想到。但是,我尽量避免修改原始结构。我最终得到了不同的解决方案,请参阅我自己的答案。
    猜你喜欢
    • 1970-01-01
    • 2019-12-27
    • 2020-10-26
    • 1970-01-01
    • 1970-01-01
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 2014-06-11
    相关资源
    最近更新 更多