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 8411143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:22:00+00:00 2026-06-10T00:22:00+00:00

The following code does not compile and I don’t know why: #include <type_traits> //

  • 0

The following code does not compile and I don’t know why:

#include <type_traits>

// Base class definition
template<template<typename> class CRTP, typename T> class Base
{
    // Friend function declaration
    public:
        template<template<typename> class CRTP0, typename T0, class>
        friend int func(const Base<CRTP0, T0>& rhs);

    // Protected test variable
    protected:
         int n;
};

// Friend function definition
template<template<typename> class CRTP0, typename T0,
class = typename std::enable_if<true>::type>
int func(const Base<CRTP0, T0>& rhs)
{
    return rhs.n;
}

// Derived class definition
template<typename T> class Derived : public Base<Derived, T> {};

// Main
int main()
{
    Derived<int> x;
    func(x);
    return 0;
}

GCC 4.6.2 (and GCC 4.7.1 on LWS) tells me that :

error: 'int Base<Derived, int>::n' is protected

Meaning that basically, the friendship is not correctly detected. As this is just an extract of my code, I would like to put the definition of the friend function outside of the class definition, like here. So, what is the problem and how to solve it ?

EDIT : I’ve modified the code to try to isolate the problem and make it far more readable. The current enable_if is always true but for my real code, I will have a “real” condition, here it is just to isolate the problem.

EDIT2 : The liveworkspace is here : friend function problem

  • 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-10T00:22:01+00:00Added an answer on June 10, 2026 at 12:22 am

    I had similar problems with gcc. I think this is a compiler bug: Because your function template has three template parameters rather than the two in the friend declaration, it doesn’t match them. In order for your intentions to work correctly with gcc, you must match the friend declaration exactly. This is most easily achieved by using the SFINAE on the function return type

     // Friend function definition
     template<template<typename> class CRTP0, typename T0>
     typename std::enable_if<true,int>::type
     func(const Base<CRTP0, T0>& rhs)
     {
        return rhs.n;
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code does not compile: class OuterClass<T> { class BaseClass { } class
The following piece of C# code does not compile: public class A { public
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
The following code does not compile, saying error C2248: 'A::getMe' : cannot access private
The following code does not compile: //int a = ... int? b = (int?)
The following code does not want to compile. See the included error message. Code:
The following code doesn't work (of course), because the marked line does not compile:
The following c++ code does not compile: int main() { double a = abs(5.1);
Why does the following code compile in C++11 and does not in C++03 ?
The following code does not time out in Ruby 1.9.3p194 (2012-04-20) [i386-mingw32]: require 'timeout'

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.