Search code examples
c++c++11typedefusing

Is this using alias for a function type (not pointer) the same as this typedef


Are these the same?

using on_receipt = void (const string);

typedef void on_receipt(const string);

These are not aliasing a function pointer, but rather an actual function type. They both compile fine. What seems odd is that the typedef version at least has the placeholder for the name of the function, but since the using moves this placeholder before the =, there is nothing separating the return and the parameters, which looks like it could be incorrect or misleading.


Solution

  • Yes they are:

    [dcl.typedef]

    2 A typedef-name can also be introduced by an alias-declaration. The identifier following the using keyword becomes a typedef-name and the optional attribute-specifier-seq following the identifier appertains to that typedef-name. It has the same semantics as if it were introduced by the typedef specifier. In particular, it does not define a new type.

    Live Example

    #include <string>
    #include <type_traits>
    
    int main()
    {
        using on_receipt1 = void (const std::string);    
        typedef void on_receipt2(const std::string);
        static_assert(std::is_same<on_receipt1, on_receipt2>::value, "");
    }