Compile same source with diffrent aliases

Ignacio Montesino Valle

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?

Botje

Given the following template file: (call it algorithm.cpp.tmpl)

using Point = @_flavor@;
using Vector = @_flavor@;
#include "algorithm.cpp"

You can have CMake generate flavors automatically and build them as part of some_target:

set(FLAVORS Eigen::Vector2d Eigen::Vector3d)
foreach(_flavor ${FLAVORS})
  string(MAKE_C_IDENTIFIER ${_flavor} _flavor_slug)
  set(_flavor_out algorithm_${_flavor_slug}.cpp)
  configure_file(algorithm.cpp.tmpl ${_flavor_out} @ONLY)
  target_sources(some_target PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/${_flavor_out})
endforeach()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Session have same equal result but must diffrent

Two notifications with same ID but diffrent Noification channels

Use same object instance(log) in diffrent files

Is it possible to do diffrent Selects in the same query?

SVG path not showing same diffrent browser

Python Mysql select same value on diffrent table

diffrent return value for same arguement of ceil() function

Cucumber the same random class in diffrent scenarios

Compile same piece of source code with different Instruction Set Architecture

Compile two versions of a document from the same latex source

How to remove same combination (same number but diffrent order) on list?

Multiple Aliases triggering same function

I need same height on <li> tag from diffrent <ul>

Is there a way to parse XML even though same Tag Names in diffrent Nodes

Need diffrent Colors on the Legend for same sereis field in ssrs

import same random number in diffrent classes for diff variable

Starting same main javascript form diffrent url's using requirejs

Need to select sum of the same field in SQL with diffrent Conditions

How to add the data in the same row but in diffrent columns according to the hotel names

Can i make different variables of same values point to diffrent objects?

Images with diffrent sizes to the same size using css only

How to remove the same pair with diffrent order in values inside dictionary python?

express path issues when using same logic on diffrent paths

SQL Select the same row multible times but with diffrent queries

Scraping three diffrent texts in three same html codes

What is the best way to have different configurations compile with different preprocessor flags on the same source code?

compile workrave from source

OpenSSL - compile out of source

Maven - No source to compile warning