根据参数的默认值启用模板参数类型的自动推导
Enabling automatic deduction of template argument type based on that argument's default value
这是我想要做的:
#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;
}
在考虑默认参数之前执行模板推导。此外,lambda 不允许出现在未计算的操作数中。
您可以先将默认函数赋给一个变量。然后你可以拼出它的类型。例如:
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);
}
现在您可以照常使用该功能了:
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
}
您不需要明确指定类型。
注意:根据 @StoryTeller,如果您在 header 中使用它,可能会导致 ODR 违规。在这种情况下,您可以使用命名仿函数类型:
struct Positive {
constexpr bool operator(int x) const
{
return x > 0;
}
};
inline constexpr Positive default_functor{};
template <typename T, typename F = decltype(default_functor)>
auto function(T x, F f = default_functor)
{
return f(x);
}
without changing the function name in the client code.
您可以重载函数模板。无需使用其他名称。
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;});
}
您不能使默认函数参数有助于模板参数推导。不允许这样做,因为它会在推导过程中引发一些难以解决的问题。
这是我想要做的:
#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;
}
在考虑默认参数之前执行模板推导。此外,lambda 不允许出现在未计算的操作数中。
您可以先将默认函数赋给一个变量。然后你可以拼出它的类型。例如:
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);
}
现在您可以照常使用该功能了:
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
}
您不需要明确指定类型。
注意:根据 @StoryTeller,如果您在 header 中使用它,可能会导致 ODR 违规。在这种情况下,您可以使用命名仿函数类型:
struct Positive {
constexpr bool operator(int x) const
{
return x > 0;
}
};
inline constexpr Positive default_functor{};
template <typename T, typename F = decltype(default_functor)>
auto function(T x, F f = default_functor)
{
return f(x);
}
without changing the function name in the client code.
您可以重载函数模板。无需使用其他名称。
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;});
}
您不能使默认函数参数有助于模板参数推导。不允许这样做,因为它会在推导过程中引发一些难以解决的问题。