分配模板生成的类C结构具有相同的布局 [英] Assigning a template-generated class to a C struct with the same layout

查看:100
本文介绍了分配模板生成的类C结构具有相同的布局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我理解正确的话,对象'A'这样定义:

If I understand correctly, the object ’A’ defined thus:

typedef struct {
    int n;
    float *p;
} myStruct;
myStruct A;

与恰好在存储器中作为被定义为对象B相同的布局的聚合

is an aggregate with exactly the same layout in memory as the object ‘B’ defined as:

template <typename T> class myTemplateClass
{
public:
    int n;
    T*  p;
};
myTemplateClass<float> B;

那么,有没有分配的更优雅的方式。

So, is there a more elegant way of assigning

A = B;

不必写

A = *(reinterpret_cast< myStruct *>(&B));

每一次?

我询问原因是,我有其中myTemplateClass形式抱着我的数据调用库函数它暴露的形式MYSTRUCT参数的接口,从code是一个很大的更自然。

My reason for asking is that I have to call a library function which exposes an interface with arguments of the form ‘myStruct’, from code where holding my data in the form of myTemplateClass is a great deal more natural.

推荐答案

这需要一些样板。每 MYSTRUCT 两种功能,每模板两种功能

This requires a bit of boilerplate. Two functions per myStruct, and two functions per template.

MYSTRUCT 的命名空间注入这两个功能:

In the namespace of myStruct inject these two functions:

auto members( myStruct& s ) {
  return std::tie(s.n, s.p);
}
auto members( myStruct const& s ) {
  return std::tie(s.n, s.p);
}

在C ++ 11你要添加 decltype 子句来明确给出的返回值。基本上领带成员中声明的确切顺序。

in C++11 you have to add a decltype clause to give the return value explicitly. Basically tie the members in the exact order declared.

myTemplateClass ,声明朋友函数体成员,做类似的事情:

In the body of the myTemplateClass, declare a friend function members that does something similar:

template <typename T>
class myTemplateClass {
public:
  int n;
  T*  p;
  friend auto members( myTemplateClass<T>& self ) {
    return std::tie( self.n, self.p );
  }
  friend auto members( myTemplateClass<T> const& self ) {
    return std::tie( self.n, self.p );
  }
};    

最后,写分配:

template<class Lhs, class Rhs>
void assign_by_members( Lhs& lhs, Rhs const& rhs ) {
  members(lhs) = members(rhs);
}

和我们完成。

任何声明无功能成员返回一些分配给其他成员的作品。 领带做逐元素的引用分配,所以一切都很好。

Anyone that declares a free function members that returns something assignable to the other members works. tie does element-wise assign on references, so all is good.

请注意,只有一个被分配的需要常量和放大器; 成员超载只有一个被分配的需要的&安培; 成员超载。所以,如果分配总是从模板类转到C - 结构,可以减少一半的样板

Note that only the one being assigned from needs the const& overload of members and only the one being assigned to needs the & overload of members. So if assignment always goes from template class to C-struct, you can halve that boilerplate.

如果你不喜欢 assign_by_members 语法,可以覆盖运营商0 如下:

If you don't like the assign_by_members syntax, you can override operator O as follows:

template <typename T>
class myTemplateClass {
public:
// ... see above for code that goes here
  template<class O,class=decltype(members(std::declval<O>())>
  operator O() const {
    O retval;
    assign_by_members( retval, *this );
    return retval;
  }
};

也做了测试,以确定类型转换为支持成员。你可以进一步和测试​​走了一步,如果成员返回值可以从成员的返回值(*此),但增加了更多的样板。

which also does a test to determine if the type converted to supports members. You could go a step further and test if the members return value can be assigned to from the return value of members(*this), but that adds more boilerplate.

这篇关于分配模板生成的类C结构具有相同的布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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