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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:59:55+00:00 2026-05-30T02:59:55+00:00

Consider this code: template<typename T> class Base { template<typename U> friend void f(void *ptr)

  • 0

Consider this code:

template<typename T>
class Base
{
   template<typename U>
   friend void f(void *ptr) {
     static_cast<Base<U>*>(ptr)->run();
   }
   protected:
       virtual void run() = 0; 
};
 
class A : public Base<A>
{
   protected:
       virtual void run() {}
};
 
/*
class B : public Base<B>
{
   protected:
       virtual void run() {}
};
*/

It compiles fine now (ideone). But if I uncomment the definition of B, then it gives the following error (ideone):

prog.cpp: In instantiation of ‘Base<B>’:
prog.cpp:20:   instantiated from here
prog.cpp:6: error: redefinition of ‘template<class U> void f(void*)’
prog.cpp:6: error: ‘template<class U> void f(void*)’ previously defined here

I know (well,I think I know) the reason why it gives this error.

So my question is :

How to avoid redefinition error in case of in-class definition of friend function template?

As long as I provide the definition of the primary template (not specialization) inside the class, I will get this error. There is also another problem with defining primary template in this way: it makes all instantiations of f function template friend of all instantiations of Base class template, which I also would like to avoid. I want to make f<T> a friend of Base<T> but not f<U> a friend of Base<T> if U and T are not same. At the same time, I also want to provide the definition inside the class. Is it possible?

  • 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-05-30T02:59:57+00:00Added an answer on May 30, 2026 at 2:59 am

    Do you really need to define f into the class? If you define it outside, your problem disappears and you can also enforce the one-to-one relationship you want (i.e. only f<T> is a friend of Base<T>):

    template <typename T> class Base;
    
    template <typename U>
    void f(void *ptr) {
       static_cast<Base<U>*>(ptr)->run();
    }
    
    template<typename T>
    class Base
    {
       friend void f<T>(void *ptr); //only one instanciation is a friend
    
       protected:
         virtual void run() = 0; 
    };
    

    However, note that the fact that only f<T> is a friend of Base<T> will not prevent the following code from compiling:

    B b;
    f<A>(&b); // compiles, f<A> calls Base<A>::run, but the cast is wrong
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: template <typename T> class String { public: ... String(T* initStr) {
Please consider this code: #include <iostream> template<typename T> void f(T x) { std::cout <<
Consider the following code: class MyClass { template <typename Datatype> friend MyClass& operator<<(MyClass& MyClassReference,
Consider: template <typename T> class Base { public: static const bool ZEROFILL = true;
Consider this code: template <int N> struct X { friend void f(X *) {}
Please consider this code: template<typename T> char (&f(T[1]))[1]; template<typename T> char (&f(...))[2]; int main()
consider this code block public void ManageInstalledComponentsUpdate() { IUpdateView view = new UpdaterForm(); BackgroundWorker
Consider the following class: class MyClass { public: template<class T> typename T::result_type apply(T& func)
Please consider the following code: #include <iostream> #include <typeinfo> template< typename Type > void
Consider this code: #include <iostream> template<class C> struct time { }; int main() {

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.