Есть ли способ заставить вывод шаблонов работать с (неявным) преобразованием? Как в следующем примере:
Код: Выделить всё
template struct A {};
template struct B
{
B(A); // implicit A->B conversion
};
template void fun(B...);
int main()
{
A a;
fun(B(a)); // works
fun(a); // does not work (deduction failure)
}
- If is a subclass of
Код: Выделить всё
A, everything works. That means that deduction can do implicit conversion using upcasting. So it seems weird that it can not do implicit conversion using a constructor.Код: Выделить всё
B - Overloading for
Код: Выделить всё
funandКод: Выделить всё
Ais possible in principle, but for multiple parameters, there are just too many combinationsКод: Выделить всё
B - Adding a deduction guideline () does not change anything.
Код: Выделить всё
template B(A)->B;
In my actual code,
Код: Выделить всё
AКод: Выделить всё
BКод: Выделить всё
std::vectorКод: Выделить всё
std::spanКод: Выделить всё
TКод: Выделить всё
TИсточник: https://stackoverflow.com/questions/694 ... emplate-de
Мобильная версия