如何在C ++中设计库包装器? [英] How to design a library wrapper in C++?

查看:135
本文介绍了如何在C ++中设计库包装器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用一种简单的语法在C ++中设计一个包装器:

I would like to design a wrapper in C++ with a simple syntax:

Vector<double,stl> v; // aka std::vector<double>
Vector<double, eigen> w; // aka Eigen::VectorXd from Eigen library
Matrix<double, eigen> m; // aka Eigen::MatrixXd from Eigen library

但是,我无法获得这种语法,特别是对于最后两个示例.这是我包装STL向量的代码:

However, I do not manage to get this syntax, especially for the two last examples. Here is my code for the case of wrapping of STL vectors:

#ifndef WRAPPER_HPP
#define WRAPPER_HPP

#include <cstdlib>
#include <vector>

//=============
// BASE WRAPPER
//=============

template<class ScalarType, template<class,class...> class WrappedType>
class Wrapper
{
protected:
  WrappedType<ScalarType> wrapped_;
};

//==============
// STL WRAPPER
//==============

template<class ScalarType, template<class,class...> class WrappedType>
struct stl;

template<class ScalarType>
struct stl<ScalarType,std::vector> : public Wrapper<ScalarType,std::vector>
{
public:
  size_t size()
  { return this->wrapped_.size(); }
};

//=======
// VECTOR
//=======
template<class ScalarType, template<class, template<class,class...> class> class Wrapper>
using Vector = Wrapper<ScalarType,std::vector>;
// **** Problem : I should not provide "std::vector" above ****

#endif

STL包装器是一种称为stl的结构.该结构实际​​上是Wrapper类的子类.我对STL的向量有专门的stl结构.我可能还会对某些容器(列表,地图等)进行其他规范化处理.因此,在声明时

STL wrapper is a structure called stl. This structure is actually a subclass of Wrapper class. I have specialized stl structure for STL's vectors. I may do some other specalizations for some containers (lists, maps, ...). Thus, when declaring

Vector<double,stl> vec

我希望能够从一对(Vector,stl)推断出它对应于std :: vector的stl特化.但是,我无法做到这一点.每次尝试执行某些操作时,我都会获得模板参数的无限递归.

I would like to be able to deduce from the pair (Vector, stl) that it corresponds to the specialization of stl for std::vector. However, I do not manage to do it. Each time I try something, I get an infinite recursion over template parameters.

也许可以使用一些typedef或模板别名来做到这一点,但是我找不到它.可能是这样的:

There may be a good way to do this with some typedef or template alias but I cannot find it. It may be something like that:

template<class ScalarType, template<class, template<class,class...> class> class Wrapper>
using Vector = Wrapper<ScalarType,????>;

哪里???等同于std :: vector,但从包装器中推导出来.但是我不知道是否可能.

where ???? would be something equivalent to std::vector, but deduced from Wrapper. But I do not know if it is possible.

我的设计也可能很幼稚.如果有任何改进建议,我将不胜感激.我只对代码的语法感兴趣.

My design may also be naive. I would really appreciate any suggestion to improve it. I am only interested in the syntax of the code.

谢谢!

推荐答案

使用C ++ 11或更高版本的语法相当容易.

This is fairly easy to do using C++11 syntax, or higher.

这是实现Vector包装器的示例,这将导致您要查找的语法.使用类似的方法来实现Matrix

Here's an example of implementing a Vector wrapper, that will result in the syntax that you're looking for. Use a similar approach to implement Matrix, etc.

该方法将模板专业化与template using结合在一起,选择正确的专业化来声明成员类型,该成员类型是要声明的实际类型:

The approach combines template specialization, with template using, selecting the correct specialization that declares a member type that's the actual type being declared:

#include <vector>

class stl; // dummy class
class eigen; // dummy class

template<typename ...Args> class vector_impl;

template<>
class vector_impl<double, stl> {

public:
    typedef std::vector<double> impl_type;
};

template<>
class vector_impl<double, eigen> {

public:
    typedef Eigen::VectorXd impl_type;   // I presume...
};

// And the magical alias:

template<typename ...Args>
using Vector=typename vector_impl<Args...>::impl_type;

定义了上面的内容

Vector<double, stl> a;    // This declares a std::vector<double>
Vector<double, eigen> b;  // This declares an `Eigen::VectorXd`

使用可变参数模板可以做一些进一步的工作,即一些其他调整也可能导致一些额外的变化,例如将自定义分配器类型转发到std::vector等...

Using variadic templates allows for some further work, namely a few additional tweaks can also result in some extras, like forwarding custom allocator types to std::vector, etc...

这篇关于如何在C ++中设计库包装器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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