10

Please consider the code:

#include    <iostream>

using namespace std;

extern  "C"
void    foo( void );

namespace   A
{
    template< int No >
    class   Bar
    {
    private:
        friend  void    ::foo( void );

        static void private_func( int n );
    };

    template< int No >
    void    Bar< No >::private_func( int n )
    {
        cout << "A:Bar< " << No << ">::private_func( " << n << " )" << endl;
    }
}

extern  "C"
void    foo( void )
{
    A::Bar< 0 >::private_func( 1 );
}

int main( )
{
    cout << " ---- " << endl;
    foo( );
}

G++ gives:

> g++ -Wall -o extern_c extern_c.cpp
extern_c.cpp: In function ‘void foo()’:
extern_c.cpp:20:7: error: ‘static void A::Bar<No>::private_func(int) [with int No = 0]’ is private
extern_c.cpp:29:31: error: within this context

If I comment the namspace A, it will compile and run correctly.

What am I missing?

I looked related topics, but could not find any that fits in my problem.

Thanks people.


EDIT:

I am now convinced that extern "C" has nothing to do with the problem. Please ignore it.

Community
  • 1
  • 1
j4x
  • 3,595
  • 3
  • 33
  • 64

3 Answers3

4

It's a g++ bug. Exists in 4.4, fixed in 4.6.

UPD: It seems that it's triggered by a combination of template and namespace. extern "C" is not relevant, as it may be commented out and the error remains.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • `g++ (SUSE Linux) 4.5.1 20101208 [gcc-4_5-branch revision 167585]`. I will search for a 4.6 and tell you if it works. – j4x Jul 25 '11 at 19:58
  • @Mark B: The C++ standard doesn't seem to indicate that your friend declarations should be silently ignored if you are a class template enclosed in a namespace. I conclude that a compiler that does it must be in error. I cannot prove it, except possibly by quoting the entire standard. – n. m. could be an AI Jul 25 '11 at 20:13
2

I don't know the explanation, but if you put foo( ) into a namespace, it works.

#include    <iostream>

using namespace std;

namespace C
{
    extern  "C"
    void    foo( void );
}

namespace   A
{
    template< int No >
    class   Bar
    {
    private:
        friend  void    C::foo( void );

        static void private_func( int n );
    };

    template< int No >
    void    Bar< No >::private_func( int n )
    {
        cout << "A::Bar< " << No << ">::private_func( " << n << " )" << endl;
    }
}


namespace C
{
    extern  "C"
    void    foo( void )
    {
        A::Bar< 0 >::private_func( 1 );
    }
}

int main( )
{
    cout << " ---- " << endl;
    C::foo( );
}

And the result:

bbcaponi@bbcaponi friends]$ g++ -Wall namespace_friend.cpp -o namespace_friend
[bbcaponi@bbcaponi friends]$ ./namespace_friend
 ----
A::Bar< 0>::private_func( 1 )
Bruno Caponi
  • 474
  • 4
  • 6
  • It won't be easy to put my real `foo()` on a namespace because it is an Interrupt Service Routine, but I'll try it anyway. Thanks. – j4x Jul 25 '11 at 20:28
  • 1
    I think this guy have the same problem, but neither has been answered: http://stackoverflow.com/questions/2236712/how-do-i-define-a-friend-class-from-the-global-namespace-in-another-namespace – Bruno Caponi Jul 25 '11 at 20:33
  • It seems to be the same problem, but the empty namespace suggestion does not work. I see the same error. – j4x Jul 25 '11 at 20:40
  • Congratulations! I had a hard work to move my assmbly startup to C++, so I could put my ISR name inside a namespace, but... it worked!!! Thanks boy, I owe you a beer. – j4x Jul 25 '11 at 21:06
0

You need to declare the friend as extern "C". Your current friend declaration finds a friend foo in the global namespace that has C++ name mangling.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • O-ho. If I change line 14 to `friend extern "C" void ::foo( void );` I receive the additional error `extern_c.cpp:14:22: error: expected unqualified-id before string constant`. – j4x Jul 25 '11 at 19:44