【发布时间】:2022-07-20 17:57:27
【问题描述】:
一位学生问我以下问题:函数的参数是否可以根据模板参数是可选的?
也就是说,给定:
struct Car{};
struct Plane{};
struct OptionCar{};
struct OptionPlane1{};
struct OptionPlane2{};
template <typename T, typename U>
void foo(const T& a, const T& b, U option /* = ??? */) ;
应该可以使用默认参数调用 Car:
Car c;
foo(c, c); // invokes foo(const Car&, const Car&, OptionCar);
但不适用于飞机:
Plane p;
foo(p, p); // compile error
foo(p, p, OptionPlane1{}); // ok
foo(p, p, OptionPlane2{}); // ok
【问题讨论】:
标签: c++