1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include <cmath> template<typename X> class A { X a; public: A(X x) : a(x) {} template<typename Y> auto diff(A<Y> const&y) const -> decltype(a-y.a) { return a-y.a; } }; template<typename X, typename Y> inline auto dist(A<X> const&x, A<Y> const&y) -> decltype(std::abs(x.diff(y))) { return std::abs(x.diff(y)); } int main() { A<double> x(2.0), y(4.5); return (int) dist(x,y); } |
I2luY2x1ZGUgPGNtYXRoPgogICAgCiAgICB0ZW1wbGF0ZTx0eXBlbmFtZSBYPiBjbGFzcyBBIHsKICAgICAgWCBhOwogICAgcHVibGljOgogICAgICBBKFggeCkgOiBhKHgpIHt9CiAgICAgIHRlbXBsYXRlPHR5cGVuYW1lIFk+CiAgICAgIGF1dG8gZGlmZihBPFk+IGNvbnN0JnkpIGNvbnN0IC0+IGRlY2x0eXBlKGEteS5hKQogICAgICB7IHJldHVybiBhLXkuYTsgfQogICAgfTsKICAgIAogICAgdGVtcGxhdGU8dHlwZW5hbWUgWCwgdHlwZW5hbWUgWT4KICAgIGlubGluZSBhdXRvIGRpc3QoQTxYPiBjb25zdCZ4LCBBPFk+IGNvbnN0JnkpIC0+IGRlY2x0eXBlKHN0ZDo6YWJzKHguZGlmZih5KSkpCiAgICB7IHJldHVybiBzdGQ6OmFicyh4LmRpZmYoeSkpOyB9CiAgICAKICAgIGludCBtYWluKCkKICAgIHsKICAgICAgQTxkb3VibGU+IHgoMi4wKSwgeSg0LjUpOwogICAgICByZXR1cm4gKGludCkgZGlzdCh4LHkpOwogICAgfQ==
prog.cpp: In function 'int main()': prog.cpp:4:9: error: 'double A<double>::a' is private prog.cpp:19:28: error: within this context prog.cpp:4:9: error: 'double A<double>::a' is private prog.cpp:19:28: error: within this context prog.cpp: In function 'decltype (std::abs(x->diff(y))) dist(const A<X>&, const A<Y>&) [with X = double, Y = double, decltype (std::abs(x->diff(y))) = double]': prog.cpp:4:9: error: 'double A<double>::a' is private prog.cpp:19:28: error: within this context prog.cpp: In function 'int main()': prog.cpp:4:9: error: 'double A<double>::a' is private prog.cpp:19:28: error: within this context prog.cpp:4:9: error: 'double A<double>::a' is private prog.cpp:19:28: error: within this context prog.cpp: In function 'decltype (std::abs(x->diff(y))) dist(const A<X>&, const A<Y>&) [with X = double, Y = double, decltype (std::abs(x->diff(y))) = double]': prog.cpp:19:28: instantiated from here prog.cpp:4:9: error: 'double A<double>::a' is private prog.cpp:13:17: error: within this context prog.cpp:4:9: error: 'double A<double>::a' is private prog.cpp:13:17: error: within this context prog.cpp:4:9: error: 'double A<double>::a' is private prog.cpp:14:32: error: within this context prog.cpp:4:9: error: 'double A<double>::a' is private prog.cpp:14:32: error: within this context
-
result: Compilation error (maybe you wish to see an example for C++11)


