Are there cases where a typedef is absolutely nece

2019-03-14 06:15发布

Consider the following excerpt from the safe bool idiom:

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

Is it possible to declare the conversion function without the typedef? The following does not compile:

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

8条回答
看我几分像从前
2楼-- · 2019-03-14 06:49

Answering the "Are there cases where a typedef is absolutely necessary?" from the question title, here is one example of where a typedef is needed:

f(unsigned char());   // compiler error!
typedef unsigned char Byte;
f(Byte());            // fine!

See the results here: http://ideone.com/JPUra

查看更多
做自己的国王
3楼-- · 2019-03-14 06:53

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

查看更多
Animai°情兽
4楼-- · 2019-03-14 06:56

Ah, I just remembered the identity meta-function. It is possible to write

operator typename identity<void (Testable::*)() const>::type() const;

with the following definition of identity:

template <typename T>
struct identity
{
    typedef T type;
};

You could argue that identity still uses a typedef, but this solution is "good" enough for me.

查看更多
太酷不给撩
5楼-- · 2019-03-14 06:59

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();
   }
};
查看更多
▲ chillily
6楼-- · 2019-03-14 06:59

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; }
};
查看更多
ゆ 、 Hurt°
7楼-- · 2019-03-14 07:06

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!

查看更多
登录 后发表回答