Ошибка с использованием decltype () в C ++ 11 (создание непрозрачного сообщения об ошибке в gcc 4.7.0)

со следующим кодом (версия моего оригинального кода)

#include <iostream>
#include <cmath>

template <typename> class A;                  // edit 1 following Mark & Matthieu
template <typename X> class A {
  X a;
  template <typename> friend class A;         // edit 1 following Mark & Matthieu
public:
  A(X x) : a(x) {}
  X get() const { return a; }                // edit 2 to avoid using A<Y>::a
  template <typename Y>
  auto diff(A<Y> const& y) const
    -> decltype(a - y.a)                       // original code causing error with gcc
    -> typename std::common_type<X, Y>::type  // alternative following Rook
    -> decltype(this->get() -                 // edit 3 not using A<X>::a
                y.get())                     // edit 2 not using A<Y>::a
  { return a - y.get(); }
};

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);
  std::cout << " dist(x,y)=" << dist(x,y) << '\n'; // <-- error here
}

Я получаю следующую ошибку с GCC 4.7.0:

test.cc: 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]’:

test.cc:5:5: error: double A<double>::a is private

highlighted line: error: within this context

Это сообщение об ошибке, очевидно, не очень полезно. Есть ли ошибка в моем коде? Или это проблема с компилятором?

EDIT1: объявление друга не помогло.

EDIT2: избегать использованияA<Y>::a тоже не помогло.

EDIT3: вместе сEDIT2 в конце концовfixed the problem,decltype() в определенииdist() требуетdecltype() заA<X>::diff()который в свою очередь использовалA<X>::a, который является частным в первом контексте.

EDTI4: Предложение ладьи об использованииtypename std::common_type<X,Y>::type тоже работает!

EDIT5но посмотрите ответ Джонатана Уэйкли наэтот вопрос

Ответы на вопрос(5)

Ваш ответ на вопрос