Программы на C++. Форум разработчиков
Гость
Как выполнить неявное преобразование, передав параметр в С++ 11?
Сообщение
Гость » 09 мар 2024, 20:41
Код: Выделить всё
template
class Prop {
public:
Prop(const std::function &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 getter;
};
When passed as a parameter to a non-template function, it does implicit conversion, but not when passed as a parameter to a template function.
Код: Выделить всё
void foo(const Prop &prop) {}
template
void bar(const Prop &prop) {}
int main() {
foo(0);
bar(0); // no instance of function template "bar" matches the argument listC/C++(304)
}
Why is this happening, and how do I make it work with parameters in template functions?
Источник:
https://stackoverflow.com/questions/781 ... ter-in-c11
1710006116
Гость
[code]template class Prop { public: Prop(const std::function &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 getter; }; [/code] When passed as a parameter to a non-template function, it does implicit conversion, but not when passed as a parameter to a template function. [code]void foo(const Prop &prop) {} template void bar(const Prop &prop) {} int main() { foo(0); bar(0); // no instance of function template "bar" matches the argument listC/C++(304) } [/code] Why is this happening, and how do I make it work with parameters in template functions? Источник: [url]https://stackoverflow.com/questions/78133233/how-to-do-implicit-conversion-by-passing-as-a-parameter-in-c11[/url]
0 Ответы
22 Просмотры
Последнее сообщение Anonymous
10 мар 2024, 05:05
0 Ответы
6 Просмотры
Последнее сообщение Anonymous
21 окт 2024, 23:00
0 Ответы
6 Просмотры
Последнее сообщение Anonymous
25 апр 2025, 23:04
0 Ответы
9 Просмотры
Последнее сообщение Anonymous
25 апр 2025, 23:47
0 Ответы
39 Просмотры
Последнее сообщение Гость
13 окт 2023, 22:48