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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:06:02+00:00 2026-06-13T22:06:02+00:00

I’ve started playing around with extern templates a bit and I’ve stumbled on an

  • 0

I’ve started playing around with extern templates a bit and I’ve stumbled on an issue which I can’t find any relevant information on. Say I have a class template with a non-template friend function (defined within the class template declaration). I declare some extern template instantiations for the class, but how do I declare the friend function as extern too?

Here is some example code:

// --- test.h ---

template <typename T>
class Foo {
  private:
    T value;
  public:
    friend void some_friend_function(Foo<T>& obj) {
      obj.value -= T(42);
    };

    void some_member_function(T rhs) { value += rhs; };

};

extern template class Foo<int>;
//extern void some_friend_function(Foo<int>&);  // I tried this also...


// --- test.cpp ---

#include "test.h"

template class Foo<int>;
//void some_friend_function(Foo<int>&);         // ... with this.

When I compile the above (with or without the commented lines), I only get the following exported symbol:

0000000000000000 W _ZN3FooIiE20some_member_functionEi

So, the non-template friend functions definitely don’t get instantiated (and extern‘d) along with the class template’s explicit instantiation. Is this normal? At least, that’s what GCC produces (tested on 4.6.3 and 4.7.2).

Is there any way that I can get the friend function to be marked extern? I know that this isn’t a huge problem, since I can happily live with the friend functions being instantiated as needed (i.e., non-extern), but I’m curious to know if there is a way to do this, if not, was it an oversight or a deliberate thing?

EDIT: The obvious workarounds

My question is specifically about non-template friend functions, not about finding a workaround to avoid the issue, which is trivial. The first obvious workaround is this:

template <typename T>
class Foo {
  private:
    T value;
  public:
    template <typename U>
    friend void some_friend_function(Foo<U>& obj) {
      obj.value -= T(42);
    };
};

extern template class Foo<int>;
extern template void some_friend_function(Foo<int>&);

// --- in cpp file: ---

template class Foo<int>;
template void some_friend_function(Foo<int>&);

And another, which matches more closely but is more troublesome, is this:

template <typename T> class Foo;  // forward-declare.

template <typename T>
void some_friend_function(Foo<T>&);  // declaration.

template <typename T>
class Foo {
  private:
    T value;
  public:
    friend void some_friend_function<>(Foo<T>& obj);  // befriend the T-specialization.
};

template <typename T>
void some_friend_function(Foo<T>& obj) {  // definition.
  obj.value -= T(42);
};

extern template class Foo<int>;
extern template void some_friend_function(Foo<int>&);

// --- in cpp file: ---

template class Foo<int>;
template void some_friend_function(Foo<int>&);
  • 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-13T22:06:03+00:00Added an answer on June 13, 2026 at 10:06 pm

    The effect of an “extern template class” is to declare an explicit instantiation to be available. The effect of explicit instantiation declarations do not apply to inline functions or template specializations (14.7.2 [temp.explicit] paragraph 10):

    Except for inline functions and class template specializations, explicit instantiation declarations have the effect of suppressing the implicit instantiation of the entity to which they refer.

    Since a friend function definition within a class definition is necessarily an inline function, it will stay an inline function independent of the explicit instantiation declaration of the template (and, as you correctly noted, it isn’t a template and doesn’t follow template instantiation rules anyway).

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

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I would like to run a str_replace or preg_replace which looks for certain words
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have an autohotkey script which looks up a word in a bilingual dictionary
I have an array which has BIG numbers and small numbers in it. I

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.