【发布时间】:2018-08-23 03:43:35
【问题描述】:
在下面的代码中,编译器无法确定我要使用哪个构造函数。为什么,我该如何解决这个问题? (Live example)
#include <tuple>
#include <functional>
#include <iostream>
template<typename data_type, typename eval_type, typename Type1, typename Type2>
class A
{
public:
using a_type = std::tuple<Type1, Type2>;
using b_type = std::tuple<std::size_t,std::size_t>;
inline explicit constexpr A(const std::function<data_type(a_type)>& Initializer,
const std::function<eval_type(data_type)>& Evaluator,
const Type1& elem1, const Type2& elem2)
{
std::cout << "idx_type" << std::endl;
}
inline explicit constexpr A(const std::function<data_type(b_type)>& Initializer,
const std::function<eval_type(data_type)>& Evaluator,
const Type1& elem1, const Type2& elem2)
{
std::cout << "point_type" << std::endl;
}
};
int main()
{
int a = 1;
long long b = 2;
auto c = A<double, double, long long, int>{
[](std::tuple<long long,int> p)->double { return 1.0*std::get<0>(p) / std::get<1>(p); },
[](double d)->double { return d; }, b,a
};
return 0;
}
【问题讨论】:
-
感谢直播 MCVE!
-
lambda 不是
std::function。 -
我不得不重新读取构造函数参数 3 次,然后才注意到
a_type和b_type不同。 -
简短的回答是这两个构造函数都可以使用,因为the fifth 重载了
std::function。 -
minimal reproducible example 实际上看起来更像this
标签: c++ templates implicit-conversion explicit-constructor