用不同的别名编译相同的来源
tldr:您可以用定义不同别名的不同标头编译相同的源代码吗?
我创建了一个库,其中一组功能使用标题中的几个别名类型编码。
algorithm_2d.h
using Point = Eigen::Vector2d;
using Vector = Eigen::Vector2d;
algorithm.cpp
Vector& scale_vector(
Vector &v,
double s)
{
double len = v.norm();
if (len != 0.0) { v *= s/len;}
return v;
}
double distance_between_points(
Point const &p1,
Point const &p2)
{
return (p2 - p1).norm();
}
现在我想编译相同的algorithm.cpp
,但具有不同的别名:
algorithm_3d.h <<
/code>
using Point = Eigen::Vector3d;
using Vector = Eigen::Vector3d;
我正在使用cmake
使用pybind11
构建,因为这主要是python库。
我宁愿不必使用模板,因为这会导致我必须在每个功能中定义每种类型。正如您在摘要中看到的那样,它将使其读取率降低,这已经是一种数学上复杂的算法(未显示的其余代码)。
编辑:
另一个示例,以清楚使用这些别名。如果我使用模板,则每次此功能都称为
.h
using PointSet = std::vector<Point, Eigen::aligned_allocator<Point> >;
.cpp
Vector compute_left_tangent(
PointSet const &pts,
int end,
{
Vector tHat1 = PointSet[end+1] - PointSet[end];
tHat1 = tHat / distance_between_points(PoinSet[end],PointSet[end+1])
}
我不想添加&lt; eigen :: vector2d, ...等等,
每次我称之为这些功能之一。也许有一种方法可以使用我缺少的模板?
TLDR: Can you compile the same source code with different headers defining diffrent aliases?
I have created a library, with a set of functions coded using a couple of aliased type in the header.
algorithm_2d.h
using Point = Eigen::Vector2d;
using Vector = Eigen::Vector2d;
algorithm.cpp
Vector& scale_vector(
Vector &v,
double s)
{
double len = v.norm();
if (len != 0.0) { v *= s/len;}
return v;
}
double distance_between_points(
Point const &p1,
Point const &p2)
{
return (p2 - p1).norm();
}
Now I would like to compile the same algorithm.cpp
but with different aliases:
algorithm_3d.h
using Point = Eigen::Vector3d;
using Vector = Eigen::Vector3d;
I am building using CMake
with pybind11
as this is mainly a python library.
I would prefer not having to use templates, as that would cause me to have to define every single type in every function. And as you can see in the snippet, it would make it a lot less readable, and this is already a mathematically complicated algorithm (rest of the code not shown).
EDIT:
Another example to make it clear what I mean by using the aliases. If I were using templates, each time this function is called
.h
using PointSet = std::vector<Point, Eigen::aligned_allocator<Point> >;
.cpp
Vector compute_left_tangent(
PointSet const &pts,
int end,
{
Vector tHat1 = PointSet[end+1] - PointSet[end];
tHat1 = tHat / distance_between_points(PoinSet[end],PointSet[end+1])
}
I don't want to have to add the <Eigen::Vector2d,..., etc>
every time I call one of these functions. Maybe there is a way of doing this with templates that I am missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
给定以下模板文件:(调用
algorithm.cpp.tmpl
)您可以使Cmake生成风味自动生成风味,并作为
some_target
的一部分构建它们:Given the following template file: (call it
algorithm.cpp.tmpl
)You can have CMake generate flavors automatically and build them as part of
some_target
:当然,这是编译器定义的一种用途。
algorithm.h
algorithm.cpp
另请参阅
https://en.cppreference.com/w/cpp/cpp/preprecessor/preprocessor/conditional
现在,您只需要告诉编译器定义2D或3D即可。
G ++ -D3D ...
我认为Microsoft编译器使用Somtehing
/d3d
Sure, this is one use of compiler defines are for.
algorithm.h
algorithm.cpp
See also
https://en.cppreference.com/w/cpp/preprocessor/conditional
Now you just need to tell your compiler to define 2D or 3D.
g++ -D3D ...
I think the microsoft compiler uses somtehing like
/D3D
模板是您的朋友。没有问题,您会担心:
必须在每个功能
template参数参数ducact 在这里做所有需要的工作。
例如:
live demo
没有代码生成器,没有宏,否
#ifdef
#ifdef s。或者,如果您喜欢更限制的模板定义(因此仅适用于eigen):此示例也很好地工作 。
如果您还有其他疑问,请以无法编译或无法正确运行的代码形式陈述它们。您可以从上面提供的Godbolt上的示例开始。
Template is your friend here. There are no problem you are so worry about:
And
Template argument deduction does all needed work here.
For example:
Live demo
No code generators, no macros, no
#ifdef
s.Or if you prefer to more restrictive templates definition (so works only with Eigen): this example works nicely too.
If you have any additional doubts please state them in form of code which do not compile or do not run properly. You can start from my examples on godbolt provided above.