C++17 如何编写is_pointer_pointer泛型 lambda

c++17 how to write is_pointer_pointer generic lambda?

本文关键字:pointer lambda 泛型 is 何编写 C++17      更新时间:2023-10-16
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。他的解决方案(又名"答案"(我已经拿走了魔杖盒,并使其更具弹性。代码在这里。

解决方案是这个 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> > ;
 };

对于知识的渴求,这里有一篇文章解释了 c++17 通用 lambda 与自动参数的问题。 提示:这就是为什么我在上面使用 std::d ecay。

在 c++14 中使用 decltypetype_traits 工具已经可以实现。

#include <type_traits>
#include <iostream>
using namespace std;

int main() {
  auto is_double_pointer = [] ( auto arg ) -> bool {
    return std::is_same<decltype(arg), double*>::value;
  };
  auto is_pointer_pointer = [] ( auto arg ) -> bool {
    return std::is_pointer<decltype(arg)>::value && 
           std::is_pointer< typename std::remove_pointer<decltype(arg)>::type >::value;
  };

  double d, *ptrd, **ptrptrd;
  std::cout << is_double_pointer(d) << 'n';
  std::cout << is_double_pointer(ptrd) << 'n';
  std::cout << is_double_pointer(ptrptrd) << 'n';
  std::cout << is_pointer_pointer(d) << 'n';
  std::cout << is_pointer_pointer(ptrd) << 'n';
  std::cout << is_pointer_pointer(ptrptrd) << 'n';
  return 0;
}

输出:

0
1
0
0
0
1

编辑:也适用于char** argv;