基于该参数的默认值启用模板参数类型的自动推导

时间:2019-04-14 08:15:00

标签: c++ templates c++17 template-deduction

这就是我想要做的:

#include <vector>

template <class ContainerType, typename ComparatorType>
void f(
    ContainerType c1,
    ComparatorType comp = 
    [](const typename ContainerType::value_type& l, const typename ContainerType::value_type& r) {return l < r;})
{
}

int main()
{
    std::vector<int> a{1, 2};
    f(a);
    return 0;
}

但这不起作用:could not deduce template argument for 'ComparatorType'

使用代理函数代替实际的默认参数值有效,但似乎过于冗长,有没有更好的方法?更不用说它是不一样的,因为现在我不能在不更改客户端代码中的函数名称的情况下用我自己的默认比较器代替

#include <vector>

template <class ContainerType, typename ComparatorType>
void f(
    ContainerType c1,
    ComparatorType comp)
{
}

template <class ContainerType>
void f2(ContainerType c)
{
    f(c, [](const typename ContainerType::value_type& l, const typename ContainerType::value_type& r) {return l < r;});
}

int main()
{
    std::vector<int> a{1, 2};
    f2(a);
    return 0;
}

2 个答案:

答案 0 :(得分:5)

  

不更改客户端代码中的函数名称。

您可以重载功能模板。无需使用其他名称。

template <class ContainerType, typename ComparatorType>
void f(
    ContainerType c1,
    ComparatorType comp)
{
}

template <class ContainerType>
void f(ContainerType c)
{
    f(c, [](const typename ContainerType::value_type& l, const typename ContainerType::value_type& r) {return l < r;});
}

您不能使默认函数参数有助于模板参数的推导。不允许这样做,因为它在推论过程中会引起一些难以解决的问题。

答案 1 :(得分:4)

在考虑默认参数之前执行模板推导。另外,lambda不允许出现在未评估的操作数中。

您可以首先将默认功能分配给变量。然后,您可以阐明其类型。例如:

{
    "registration_id": [
        "This field must be unique."
    ]
}

现在您可以照常使用该功能:

auto default_functor = [](int x){ return x > 0; };

template <typename T, typename F = decltype(default_functor)>
auto function(T x, F f = default_functor)
{
  return f(x);
}

您无需明确指定类型。


注意:根据@StoryTeller,如果在标头中使用ODR,则可能会导致违反ODR。在这种情况下,您可以使用命名的仿函数类型:

bool even(int x)
{
  return x % 2 == 0;
}

struct Odd {
  bool operator()(int x) const
  {
    return x % 2 == 1;
  }
};

void g()
{
  function(1); // use default functor
  function(1, even); // use a free function
  function(1, Odd{}); // use a function object
  function(1, [](int x){ return x < 0; }); // use another lambda
}