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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:41:08+00:00 2026-05-26T12:41:08+00:00

I have a problem with giving a template class a template friend in Sun

  • 0

I have a problem with giving a template class a template friend in Sun Studio. The code compiles fine with GNU G++ (4.4.1 and 4.4.3), but fails with Sun Studio C++ (5.9 SunOS_sparc Patch 124863-01 2007/07/25).

Here is a minimal example:

// Forward declarations
template<class T> class M;
template<class T> void f(M<T>, M<T>);

// Define M<T>
template<class T>
class M
{
public:
    void f(M<T>) { }

    friend void ::f<>(M<T>, M<T>);
};

// Define global function f
template<class T>
void f(M<T> a, M<T> b)
{
    a.f(b);
}

M<int> a;

When I try to compile it via CC -c -o t3.o t3.cpp, I get the following error messages:

"t3.cpp", line 12: Warning:  A friend function with template-id name must have a template declaration in the nearest namespace.
"t3.cpp", line 22:     Where: While specializing "M<int>".
"t3.cpp", line 22:     Where: Specialized in non-template code.
"t3.cpp", line 12: Error: Global scope has no declaration for "f".
"t3.cpp", line 22:     Where: While specializing "M<int>".
"t3.cpp", line 22:     Where: Specialized in non-template code.
1 Error(s) and 1 Warning(s) detected.

Is this a problem with Sun Studio C++, or is it invalid C++ (which is still accepted by GCC and gives no warnings with -Wall -pedantic)? Is there an elegant way to change the code such that it is standard compliant and compiles both under GCC and Sun Studio?

Thanks a lot in advance!

  • 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-26T12:41:08+00:00Added an answer on May 26, 2026 at 12:41 pm

    Successfully compiled your code using “CC: Sun C++ 5.8 Patch 121017-13 2008/01/02” adding template declaration to a friend:

    template<class T>
    class M
    {
        ...
        template <class A>
        friend void ::f(M<A>, M<A>);
        ...
    };
    

    Following is not an answer to original question but those who is looking why a friend template class causes “Error: Multiple declaration for ” error compiling using Sun CC compiler, just add forward declaration for the friend class, like this:

    template <typename T> class B; //will fail without forward declaration
    
    class A
    {
        private:
        template <typename T> friend class B;
    };
    
    template <typename T> class B {};
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem. This code is not giving the results it should. It
I have a problem in giving Ajax functionality to a hyperlink. I have files
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have an anoying problem that is giving me a hard time these days...
This problem is giving me a real headache. I have two tables in my
I have problem with this code, it's working on debug environment. On the instruments
I have problem where google chrome is showing: The site uses SSL, but Google
I have a problem where I need to tokenize my string (using java code)
I have the following code in a project I am doing for class. I
I have two classes that depend on each other: class Foo; //forward declaration template

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.