2014-02-11 56 views
0

能幹淨地從一個匹配函數typedef做一個函數指針typedef嗎?從C++中的函數typedef中製作函數指針typedef?

// Can I define fx_pt in terms of fx_t without duplicating the signature? 
// Does decltype allow for any new sort of trick here? 
using fx_t = char(int, char, std::vector<float> const &); 
using fxp_t = char(*)(int, char, std::vector<float> const &); 

// Using function typedef to enforce interface for declarations and definitions. 
fx_t foo, bar, baz; 
char foo(int) { /* ... */ } 

// Using fp typedef elsewhere as normal. 
fxp_t whatever{bar}; 
whatever(99); 

我不指望有關函數(指針)什麼類型定義的語法是對我來說完全直觀,但我已經無法確定是否消除重複的代碼是可能的。

這可能扎入更大的問題類/原始typedef和類/原始指針的typedef,我記得聽到不可移植性之間轉換之間轉換的...

回答

5

只需添加一個*給函數別名

using fx_t = char(int, char, std::vector<float> const &); 
using fxp_t = fx_t*; 

Live demo

+0

+1比我好。 :D – 0x499602D2

+0

哇,我是個白癡。謝謝! – Jeff

+1

'std :: add_pointer'值得一提的恕我直言,儘管它本質上是一樣的事情(http://en.cppreference.com/w/cpp/types/add_pointer) – zahir