2014-02-11 57 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