Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8104961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T00:02:30+00:00 2026-06-06T00:02:30+00:00

with the following code (a boiled-down version of my original code) #include <iostream> #include

  • 0

with the following code (a boiled-down version of my original code)

#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
}

I get the following error with 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

This error message is obviously not very helpful. Is there an error in my code? Or is this a problem with the compiler?

EDIT1: the friend declaration didn’t help.

EDIT2: avoiding using A<Y>::a didn’t help either.

EDIT3: together with EDIT2 finally fixed the problem. The decltype() in the definition of dist() requires the decltype() for A<X>::diff(), which in turn used A<X>::a, which is private in the first context.

EDTI4: Rook’s suggestion of using typename std::common_type<X,Y>::type also works!

EDIT5: but see Jonathan Wakely’s answer to this question

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T00:02:31+00:00Added an answer on June 6, 2026 at 12:02 am

    TL;DR: Gcc appears to have a bug where trailing return types on template member functions are not treated as within the class’s scope.

    This bug causes gcc to fail when instantiating the template member function auto diff(A<Y> const&y) const -> decltype(a-y.a) because a is private and gcc thinks private members are inaccessible here.


    The code builds fine with clang and VC++, and I don’t see anything trying to access A<double>::a outside A<double>, so it looks like a gcc bug to me.

    Others have mentioned that A<X> and A<Y> are different classes, but that’s not the case here, both are A<double>. I believe that means that friendship is not necessary in this case, although to work in the general case A<X> does need to be friends with other specializations of A.

    Specifically, a in y.a is a dependent name so it cannot be looked up until A<Y> is known. At that point lookup is done, the accessibility is tested and it should be found that A<double> does have access to A<double>::a.

    Here’s the exact code I compiled in both clang (svn-3.2) and VC++11 (Since I’m using clang on Windows I can’t #include <iostream>)

    #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);
    }
    

    This code results in build errors on gcc 4.5 similar to what you describe.

    Replacing

    auto diff(A<Y> const&y) const -> decltype(a-y.a)
    

    with

    auto diff(A<Y> const&y) const -> typename std::common_type<X,Y>::type
    

    causes the code to work on gcc 4.5.

    This indicates to me a bug where gcc is failing to treat trailing return types as inside the class’s scope. Some testing reveals that the trailing return type must be on a template function to trigger the bug.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After writing code that can be boiled down to the following: var size=-1; var
I boiled the thing I want to do to the following minimal code: public
Following code worked fine abstract class FunctionRunnable<V> implements Runnable { protected abstract V calculate();
Following code: <%= render 'shared/error_messages', f.object %> where f.object is instance of a class
Following code is generated by a for loop. <form action=saveresponse.php method=POST name=mainForm> <input class=cbox_yes
The following code compiles in MSVC++, but does not compile in GCC 4.5.1: #include
I found a bug in my code which boiled down to comparing Double(0.0) with
Consider the code in the following example: abstract class Car { public abstract Engine
Following code is simplified version of the code that I am trying to optimize.
// following code works fine n open notepad... class demo { public static void

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.