Boost.Proto:具有自定义终端类的EDSL的常规类布局
Boost.Proto: General class layout for an EDSL with custom terminal classes
为了熟悉Boost.Proto,我正在尝试通过改编用户指南中的TArray示例,为固定但任意大小的浮点向量构建另一个表达式模板库。我要做的第一件事是定义我的向量类:
typedef double real;
// Forward-declare an expression wrapper
template<typename Expr>
struct vector_expr_wrapper; // line 13
class FPVector : vector_expr_wrapper< proto::terminal< FPVector >::type > { // line 16
public:
FPVector() : numElements(0), elements(0) {}
FPVector(size_t n) : numElements(n), elements(new real[n]) {}
~FPVector() { delete[] elements; }
real& operator[](size_t i) { return elements[i]; }
template<typename Expr>
FPVector const& operator=(vector_expr_wrapper<Expr> vec_expr) {
for(size_t i=0; i<numElements; i++) {
elements[i] = vec_expr[i];
}
return *this;
}
private:
size_t numElements;
real * elements;
};
vector_expr_wrapper
还重载了其operator[]
,以使用从proto::callable_context
派生的vector_context
来评估自身,该返回FPVector
终端的vector[index]
。
当我编译代码并使用非常简单的语句(a = b + c;
(调用它时,我收到错误消息:
../main.cpp:16:18: error: invalid use of incomplete type ‘struct vector_expr_wrapper<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<FPVector>, 0l> >’
../main.cpp:13:8: error: declaration of ‘struct vector_expr_wrapper<boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<FPVector>, 0l> >’
../main.cpp: In function ‘int main()’:
../main.cpp:121:8: error: no match for ‘operator+’ in ‘b + c’
然后 G++ 列出了可能的候选者......我从中理解的是,在定义FPVector
之前,我必须给出vector_expr_wrapper
的整个定义,但我不能这样做,因为vector_expr_wrapper
中的其他一切都取决于FPVector
(语法,评估上下文......
该如何解决这个问题(即我应该如何布局我的类(?
TArray 示例通过很晚才定义它们的数组类并使用 int[3]
之前指定其类型来循环这个问题——我想我无法在我的上下文中重现。
非常感谢您的帮助!
这是我的解决方案:
#include <vector>
#include <iostream>
#include <boost/proto/proto.hpp>
#include <boost/format.hpp>
namespace proto = boost::proto;
using proto::_;
typedef double real;
struct vector_grammar
: proto::or_<
proto::terminal< std::vector<real> >
, proto::plus< vector_grammar, vector_grammar >
, proto::minus< vector_grammar, vector_grammar >
, proto::multiplies< vector_grammar, vector_grammar >
, proto::divides< vector_grammar, vector_grammar >
>
{};
template<typename Expr>
struct vector_expr;
// Tell proto that in the vector_domain, all
// expressions should be wrapped in vector_expr<> and
// must conform to the vector_grammar
struct vector_domain
: proto::domain<
// use_basic_expr here instructs proto to use the stripped-
// down proto::basic_expr instead of proto::expr to reduce
// compile times. It's not necessary.
proto::use_basic_expr<proto::pod_generator<vector_expr> >
, vector_grammar
>
{};
struct vector_subscript_context
: proto::callable_context< vector_subscript_context const >
{
typedef real result_type;
explicit vector_subscript_context(std::ptrdiff_t i)
: i_(i)
{}
// Index array terminals with our subscript. Everything
// else will be handled by the default evaluation context.
real operator ()(proto::tag::terminal, std::vector<real> const &data) const
{
return data[this->i_];
}
private:
std::ptrdiff_t i_;
};
// Forward-declare an expression wrapper
template<typename Expr>
struct vector_expr
{
BOOST_PROTO_BASIC_EXTENDS(Expr, vector_expr<Expr>, vector_domain)
// Use the vector_subscript_context to implement subscripting
// of a vector_expr expression tree.
real operator []( std::ptrdiff_t i ) const
{
vector_subscript_context const ctx(i);
return proto::eval(*this, ctx);
}
};
template<typename = proto::is_proto_expr>
struct FPVector_
: vector_expr<
proto::basic_expr<
proto::tag::terminal
, proto::term< std::vector<real> >
>
>
{
explicit FPVector_(std::size_t n = 0)
{
proto::value(*this).resize(n);
}
real & operator[](std::ptrdiff_t i)
{
return proto::value(*this)[i];
}
real const & operator[](std::ptrdiff_t i) const
{
return proto::value(*this)[i];
}
template<typename Expr>
FPVector_ & operator=(vector_expr<Expr> const & that)
{
std::ptrdiff_t const size =
static_cast<std::ptrdiff_t>(proto::value(*this).size());
for(std::ptrdiff_t i = 0; i < size; ++i)
proto::value(*this)[i] = that[i];
return *this;
}
};
typedef FPVector_<> FPVector;
int main()
{
FPVector a(3), b(3), c(3);
for(std::ptrdiff_t i = 0; i < 3; ++i)
b[i] = c[i] = i;
a = b + c;
std::cout
<< boost::format("a = {%d, %d, %d}") % a[0] %a[1] %a[2]
<< std::endl;
}
您显示的代码存在许多问题:
// Forward-declare an expression wrapper
template<typename Expr>
struct vector_expr_wrapper; // line 13
class FPVector : vector_expr_wrapper< proto::terminal< FPVector >::type >
您不能从不完整的类模板(vector_expr_wrapper
(从(具体(类(FPVector
(继承。此外,您的FPVector
类型正在尝试包含自身的副本。 proto::terminal
是对象类型的包装器。把它想象成事物本身的替身,但有一些额外的原始香料。然后从它那里继承或从它继承的东西是不会飞的。
我发布的代码中唯一真正的技巧是使用 proto::is_proto_expr
.这是一个令人讨厌的黑客,可以让 ADL 启动查找在 boost::proto
命名空间中定义的运算符重载。在 proto 文档的"扩展<>表达式包装器"部分底部的警告中详细描述了这件事。
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 具有瞬态资源的RAII类
- 具有默认模板参数的多态类的模板推导失败
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 将二维矢量传递给类
- std::具有相同基类的类的变体
- 命名空间中具有.h和.cpp文件的类
- 在c++中使用nlohmann从类到json的转换
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 在C++中,为什么使用静态类函数而不是常规函数?
- C++ - 模板类专用化是否有办法包含常规模板中的代码
- 是否可以将一个类别的插槽或常规功能从一个类连接到另一个类的插槽或常规功能?(QT)
- 使用Typedef和常规类定义的匿名类定义的等效率
- 将类用于功能而不是使用常规功能是不好的做法吗?
- 如何在不同的类中使用QNetworkAccessManager?类之间重要数据的常规共享
- Boost.Proto:具有自定义终端类的EDSL的常规类布局
- 常规"字符串"类"rwcstring"类之间的区别
- 如何获取常规函数指针作为 C++ 类的私有成员
- 同时具有指针类型和常规类型的类模板
- 将模板类更改为常规类- c++