template <typename T>
class Prop {
public:
Prop(const std::function<T()> &getter) : getter(getter) {}
Prop(const T &value) : getter([=]() { return value; }) {}
Prop(const char *value) : getter([=]() { return std::string(value); }) {}
T operator()() const {
return getter();
}
private:
const std::function<T()> getter;
};
当作为参数传递给非模板函数时,它会被隐式转换,但当作为参数传递给模板函数时则不会。
void foo(const Prop<int> &prop) {}
template <typename T>
void bar(const Prop<T> &prop) {}
int main() {
foo(0);
bar(0); // no instance of function template "bar" matches the argument listC/C++(304)
}
为什么会发生这种情况,如何使其与模板函数中的参数一起工作?
我只是回答这个问题,因为我想提出我的疑问..很抱歉