Are there cases where a typedef is absolutely necessary?

后端 未结 8 1167
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-05 01:21

Consider the following excerpt from the safe bool idiom:

typedef void (Testable::*bool_type)() const;
operator bool_type() const;

Is it possibl

相关标签:
8条回答
  • 2021-02-05 01:49

    In C++11, you can do it like this (gcc 4.5.2):

    operator decltype((void (Testable::*)() const)(0))() const ;
    

    I'm not saying it's pretty...

    0 讨论(0)
  • 2021-02-05 01:53

    It seems that the grammar demands using a typedef in your case. A conversion-function-id must be of the form operator conversion-type-id. The conversion-type-id cannot contain parentheses. So you must use typedef when converting to a pointer-to-function type, or to a pointer-to-member-function type.

    0 讨论(0)
  • 2021-02-05 01:56

    A typedef is not a macro your second example is not equivalent to the first. In the first case your typedef is defining a functor then using that type in a cast operator of the functor type. In the second the operator is using bad syntax as there is no operator specified because there is no type. I'm not sure how to write it but there is a way usually.

    Typedefs aren't really necessary except for making human readable code in TMP and even then it depends on what kind of human you are.

    Since I can't come up with the alternative syntax maybe typedefs are necessary in some cases. I just thought of another one possibly. Say you had a template with specializations which contained a static method with a return type like below:

    template <typename T>
    struct WhateverHandler
    {
       typedef T rType;
       static rType Whatever() { return rType(); }
    };
    
    template <>
    struct WhateverHandler<std::string>
    {
       typedef std::string rType;
       static rType Whatever() { return rType(); }
    };
    

    I think in this case also you would need the typedef in order to call the static method regardless of specialization as otherwise the method could confuse the compiler because the return types would differ but it wouldn't be a proper overload.

    template <typename T>
    struct WhateverUser
    {
       typename WhateverHandler<T>::rType DoWhatever()
       {
           return WhateverHandler<T>::template Whatever();
       }
    };
    
    0 讨论(0)
  • 2021-02-05 01:57

    I just ran across this issue, with clang++:

    foo.cpp:17:8: error: must use a typedef to declare a conversion to 'void (*(int))()'
    

    and there's a C++11 STL template which covers the identity<T> functionality:

    #include <type_traits>
    …
    struct foo {
         void bar( ) const { }
         operator std::common_type<void(foo::*)( )const>::type( ) { return &foo::bar; }
    };
    
    0 讨论(0)
  • 2021-02-05 02:00

    One case (unrelated to your question) where a typedef is required is when using the

    va_arg() macro. Quoting the C99 standard (7.15.1.1):

    type* va_arg(va_list ap, type);

    ...

    The parameter type shall be a type name specified such that the type of a pointer to an object that has the specified type can be obtained simply by postfixing a * to type

    0 讨论(0)
  • 2021-02-05 02:01

    My analysis says that it is not possible without using typedef. The compiler sees ( as the first token and assumes you are overloading () operator, which shouldn't have any arguments (The arguments would come in next set of parenthesis). Putting any set of extra parenthesis wouldn't help either - but would actually confuse the compiler and hence set of more errors.

    Most of the STL code is on top of typedefinitions, and we should/must use them!

    0 讨论(0)
提交回复
热议问题