Search code examples
c++constantsauto

Can `auto const*const` by typedefed into some single-word type?


I'd like to simplify typing of auto const*const construct by creating a typedef something like

// (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; };

Can I achieve something like tihs with C++? Maybe template aliases will help somehow?


Solution

  • Assuming f is a function (and not some macro that returns different types), and f returns some raw pointer type, you can use 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;
    

    Or, as a one line mumbo jumbo:

    using deepcp = std::add_const<
      std::pointer_traits<decltype(f(1))>::element_type
    >::type * const ;
    

    Yay.

    Note: I used add_const because I don't know from your example whether f returns a pointer or const pointer, i.e. if pointee_t is const or not - this way it works for both possibilities.

    [Example]