2015-03-30 79 views
0

這與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 
+0

我剝出了一些東西,並把它放在使用GCC的ideone中:http://ideone.com/NOHKz7這基本上就是你所擁有的。它似乎工作。所以我不確定你爲什麼會出錯。 – qeadz 2015-03-30 19:47:07

+0

我有點困惑,你想用靜態模板免費函數表達什麼? – Lol4t0 2015-03-30 19:47:11

+0

@qeadz - 哦,夥計......這可能意味着它是蘋果Clang中的一個bug。你知道任何解決方法嗎?如果更改無法支持現代編譯器,那麼我無法進行更改。 – jww 2015-03-30 19:50:31

回答

1

鑑於在字符串到在.cpp聲明是一個免費的功能,試試這個在標題:

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); 
}; 
+0

啊,就是這樣。我不得不在類聲明之外聲明它。謝謝。 – jww 2015-03-30 20:08:30