Search code examples
c++functiontemplatesstaticfriend

Provide friendship to static template function (non-member)?


This is similar to How to allow template function to have friend(-like) access?, but I'm working with a static template function (non-member). I'm trying to accomplish the following.

Integer.h:

class Integer {    
    ...
    // Forward declare due to static
    template <typename T> static Integer StringToInteger(const T *str, ByteOrder order);
    // Provide friendship
    template <typename T> friend Integer StringToInteger(const T *str, ByteOrder order);
};

Integer.cpp:

// T will be a char or wchar_t
template <class T>
static Integer StringToInteger(const T *str, ByteOrder order)
{
    ...
}

When declaring the friendship, the static template function is causing the error:

$ make
c++ -DDEBUG -g3 -O1 -fPIC -Wno-tautological-compare -DCRYPTOPP_DISABLE_ASM -pipe -c integer.cpp

integer.cpp:2970:16: error: static declaration of 'StringToInteger' follows
      non-static declaration
static Integer StringToInteger(const T *str, ByteOrder order)
               ^
./integer.h:380:42: note: previous declaration is here
    template <typename T> friend Integer StringToInteger(const T *str, B...

But according to Is it possible to declare a friend function as static?, I need to forward declare the function as static.

Question: How do I provide friendship to a static template function?


If I add static to the friend declaration, then I get another error:

// Forward declaration to retain static
// template <typename T> static Integer StringToInteger(const T *str, ByteOrder order);
template <typename T> friend static Integer StringToInteger(const T *str, ByteOrder order);

Then it results in:

$ make
c++ -DDEBUG -g3 -O1 -fPIC -Wno-tautological-compare -DCRYPTOPP_DISABLE_ASM -pipe -c integer.cpp

In file included from integer.cpp:8:
./integer.h:380:34: error: 'static' is invalid in friend declarations
    template <typename T> friend static Integer StringToInteger(const T ...

If I try to declare friendship on a specialization:

// Forward declaration to retain static
template <typename T> static Integer StringToInteger(const T *str, ByteOrder order);
friend Integer StringToInteger<char>(const char *str, ByteOrder order);

Then I get another error:

$ make
c++ -DDEBUG -g3 -O1 -fPIC -Wno-tautological-compare -DCRYPTOPP_DISABLE_ASM -pipe -c integer.cpp

In file included from integer.cpp:8:
./integer.h:381:20: error: no function template matches function template
      specialization 'StringToInteger'
    friend Integer StringToInteger<char>(const char *str, ByteOrder order);

$ c++ --version
Apple LLVM version 5.1 (clang-503.0.40) (based on LLVM 3.4svn)
Target: x86_64-apple-darwin12.6.0
Thread model: posix

Solution

  • Given the StringToInteger declared in the .cpp is a free function, try this in the header:

    class Integer;
    
    // Forward declare due to static
    template <typename T> static Integer StringToInteger(const T *str);
    
    class Integer {    
      public:
      // Provide friendship
      template <typename T> friend Integer StringToInteger(const T *str);
    };