使用具有模板化函数的自动类型&lambda和 - 使用g ++ 5.4的自动模板推导

时间:2017-08-21 22:20:10

标签: c++ c++11 templates lambda g++

我正在与编译器及其使用自动模板扣除的能力作斗争。以下内容无法编译,模板自动扣除失败:

template<typename Configuration, typename ConfigAlloc = std::allocator<Configuration>>
std::vector<Configuration, ConfigAlloc> ResamplePath(
        const std::vector<Configuration, ConfigAlloc>& path,
        const double resampled_state_distance,
        const std::function<double(const Configuration&, const Configuration&)>& state_distance_fn,
        const std::function<Configuration(const Configuration&, const Configuration&, const double)>& state_interpolation_fn)
{
    ...
}

void foo()
{
    std::vector<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d>> path;
    ...

    const auto interpolation_fn = [&] (
            const Eigen::Vector3d& prev,
            const Eigen::Vector3d& curr,
            const double ratio)
    {
        Eigen::Vector3d interpolated;
        ...
        return interpolated;
    };

    auto resample_result = ResamplePath(path, ..., interpolation_fn);
}

如果我将lambda声明更改为

    const std::function<Eigen::Vector3d(const Eigen::Vector3d&, const Eigen::Vector3d&, const double)> interpolation_fn = [&] (
            const Eigen::Vector3d& prev,
            const Eigen::Vector3d& curr,
            const double ratio)
    {
        Eigen::Vector3d interpolated;
        ...
        return interpolated;
    };

或我更改函数调用以显式定义类型

auto resample_result = ResamplePath<Eigen::Vector3d, Eigen::aligned_allocator<Eigen::Vector3d>>(
                path, ..., interpolation_fn);

然后一切都编译没有任何问题。

我是否遗漏了一些可能允许自动生成工作而没有明确定义lambda类型的东西?

编辑:确切的编译错误

virtual_rubber_band.cpp: In member function ‘void smmap::VirtualRubberBand::resampleBand(bool)’:
virtual_rubber_band.cpp:279:111: error: no matching function for call to ‘ResamplePath(EigenHelpers::VectorVector3d&, const double&, const smmap::VirtualRubberBand::resampleBand(bool)::<lambda(const Vector3d&, const Vector3d&)>&, const smmap::VirtualRubberBand::resampleBand(bool)::<lambda(const Vector3d&, const Vector3d&, double)>&)’
                 band_, max_distance_between_rubber_band_points_, state_distance_fn, state_interpolation_fn);
In file included from virtual_rubber_band.cpp:3:0:
shortcut_smoothing.hpp:191:52: note: candidate: template<class Configuration, class ConfigAlloc> std::vector<_Tp, _Alloc> shortcut_smoothing::ResamplePath(const std::vector<_Tp, _Alloc>&, double, const std::function<double(const Datatype&, const Datatype&)>&, const std::function<Configuration(const Configuration&, const Configuration&, double)>&)
 inline std::vector<Configuration, ConfigAlloc> ResamplePath(
                                                ^
shortcut_smoothing.hpp:191:52: note:   template argument deduction/substitution failed:
virtual_rubber_band.cpp:279:111: note:   ‘const smmap::VirtualRubberBand::resampleBand(bool)::<lambda(const Vector3d&, const Vector3d&)>’ is not derived from ‘const std::function<double(const Datatype&, const Datatype&)>’
                 band_, max_distance_between_rubber_band_points_, state_distance_fn, state_interpolation_fn);

1 个答案:

答案 0 :(得分:1)

lambda 不是标准函数

std函数是一种类型橡皮擦。除了“用签名调用”,复制和销毁(加上不重要的东西)之外,它会删除ecerything。

类型推导和类型擦除是相反的操作。推断类型擦除类型是代码味道。

template<typename Configuration, typename ConfigAlloc = std::allocator<Configuration>,
  class Distance, class Interpolate>
std::vector<Configuration, ConfigAlloc> ResamplePath(
    const std::vector<Configuration, ConfigAlloc>& path,
    const double resampled_state_distance,
    const Distance& state_distance_fn,
    const Interpolate& state_interpolation_fn)

现在我们摆脱了试图推断如何键入擦除,并...停止类型擦除。这可能导致运行时性能提高,因为编译器发现内联类型擦除很棘手。

template<typename Configuration, typename ConfigAlloc = std::allocator<Configuration>>
std::vector<Configuration, ConfigAlloc> ResamplePath(
    const std::vector<Configuration, ConfigAlloc>& path,
    const double resampled_state_distance,
    block_deduction<const std::function<double(const Configuration&, const Configuration&)>&> state_distance_fn,
    block_deduction<const std::function<Configuration(const Configuration&, const Configuration&, const double)>&> state_interpolation_fn)

其中block_deduction阻止编译器尝试使用这些参数推断。

template<class T>struct tag_t{using type=T;};
template<class T>using block_deduction=typename tag_t<T>::type;

您真正的问题可能是您希望使用std::function作为一个概念 - 您希望该参数与该签名兼容 - 这还不是C ++的一部分。概念可能会在中到达。