【发布时间】:2018-04-17 12:55:46
【问题描述】:
auto is_pointer_pointer = [] ( auto arg ) -> bool {
// implementation here
}
一种方法如何实现这个通用 lambda?
用法示例:
int main ( int argc, char ** argv ) {
auto dp = is_pointer_pointer(argv) ; // returns true
}
解决方案
感谢@luk32。他的解决方案(又名“答案”)我采用了魔杖盒,并使其更具弹性。代码是here。
解决方案是这个 lambda:
// return true if argument given is
// pointer to pointer of it's type T
// T ** arg
auto is_pointer_pointer = [&] ( const auto & arg ) constexpr -> bool {
using arg_type = std::decay_t< decltype(arg) > ;
return std::is_pointer_v<arg_type> &&
std::is_pointer_v< std::remove_pointer_t<arg_type> > ;
};
对于知识的渴求here 是解释带有自动参数的 c++17 通用 lambda 问题的文章。提示:这就是我在上面使用 std::decay 的原因。
【问题讨论】:
-
如果 arg 的类型为
double*,您的意思是返回 true?还是某种T**? -
您可以将解决方案放在答案中,也许这样会更清楚一些。如果您有原始解决方案,通常会回答您自己的问题。
-
我没有原始解决方案,我采用了@luk32 解决方案并使其更具弹性。您是否对他的解决方案(即“答案”)投了赞成票?
标签: c++ lambda c++17 generic-programming