如何在 C++11 中通过作为参数传递来隐式转换?

问题描述 投票:0回答:1
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)
}

为什么会发生这种情况,如何使其与模板函数中的参数一起工作?

c++ c++11
1个回答
0
投票

我只是回答这个问题,因为我想提出我的疑问..很抱歉

© www.soinside.com 2019 - 2024. All rights reserved.