我正在尝试在“垂直()”函数中使用模板参数推导:
#include <iostream>
template <typename component = double>
struct offset {
component x;
component y;
};
template <typename component>
offset(component x, component y) -> offset<component>;
template <typename component>
offset<component> perpendicular(offset<component> const &o) {
return offset{o.y, -o.x};
}
template <typename component>
std::ostream &operator<<(std::ostream &s, offset<component> const &o) {
return s << '(' << o.x << ", " << o.y << ')';
}
int main() {
std::cout << perpendicular({3.1, 1.2}) << '\n';
return 0;
}
然而,这不会编译;铿锵声(带-std='c++17'
)说:candidate template ignored: couldn't infer template argument 'component' offset<component> perpendicular(offset<component> const &o) {
。
我应该辞职写作perpendicular(offset{1.0, 2.0})
还是有办法给编译器一个提示?