我想通过创建类似 typedef 的东西来简化 auto const*const
构造的类型
// (pseudocode)
using deepcp=auto const*const;
deepcp a=f(1),b=f(2),c=f(3);
auto lam=[](deepcp x,deepcp y,deepcp z){ return *x+*y+*z; };
我可以用 C++ 实现类似 tihs 的东西吗?也许模板别名会有所帮助?
最佳答案
假设 f
是一个函数(而不是一些返回不同类型的宏),并且 f
返回一些原始指针类型,您可以使用 decltype:
using ret_f_t = decltype(f(1));
using pointee_t = std::pointer_traits<ret_f_t>::element_type;
using deepcp std::add_const<pointee_t>::type * const;
或者,作为一行胡言乱语:
using deepcp = std::add_const<
std::pointer_traits<decltype(f(1))>::element_type
>::type * const ;
是的。
注意:我使用 add_const
因为我不知道你的例子是 f
返回一个指针还是 const 指针,即如果 pointee_t
是否为 const - 这种方式适用于两种可能性。
关于c++ - `auto const*const` 可以通过类型定义为某种单字类型吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34720001/