c++17 如何编写 is_pointer_pointer 泛型 lambda?

c++17 how to write is_pointer_pointer generic lambda?

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。他的解决方案(又名 "Answer")我已经采用了魔杖盒,并使其更具弹性。代码是 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 的原因。

在 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;