这与How to allow template function to have friend(-like) access?类似,但我正在使用static
模板函数(非成员)。我正在努力完成以下内容。向静态模板函数(非成员)提供友谊?
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)
{
...
}
申报时的友谊,在static
模板函数导致错误:
$ 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...
但根据Is it possible to declare a friend function as static?,我需要转发声明函数为static
。
问题:如何为静态模板函数提供友谊?
如果我添加static
的朋友声明,然后我得到另一个错误:
// 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);
然后它会导致:
$ 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 ...
如果我试图声明友谊专业化:
// 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);
然后我得到另一个错误:
$ 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
我剥出了一些东西,并把它放在使用GCC的ideone中:http://ideone.com/NOHKz7这基本上就是你所拥有的。它似乎工作。所以我不确定你为什么会出错。 – qeadz 2015-03-30 19:47:07
我有点困惑,你想用静态模板免费函数表达什么? – Lol4t0 2015-03-30 19:47:11
@qeadz - 哦,伙计......这可能意味着它是苹果Clang中的一个bug。你知道任何解决方法吗?如果更改无法支持现代编译器,那么我无法进行更改。 – jww 2015-03-30 19:50:31