Difference between decltype and typeof?

前端 未结 5 631
别跟我提以往
别跟我提以往 2021-01-01 10:48

Two question regarding decltype and typeof:

  • Is there any difference between the decltype and typeof operato
相关标签:
5条回答
  • 2021-01-01 11:14

    typeof has not been standardized, although it was implemented by several compiler vendors, e.g., GCC. It became obsolete with decltype.

    A good explanation for the shortcomings of typeof can be found in this related answer.

    0 讨论(0)
  • 2021-01-01 11:19

    The difference between the two is that decltype always preserves references as part of the information, whereas typeof may not. So...

    int a = 1;
    int& ra = a;
    typeof(a) b = 1;     // int
    typeof(ra) b2 = 1;   // int
    decltype(a) b3;      // int
    decltype(ra) b4 = a; // reference to int
    

    The name typeof was the preferred choice (consistent with sizeof and alignof, and the name already used in extensions), but as you can see in the proposal N1478, concern around compatibility with existing implementations dropping references led them to giving it a distinct name.

    "We use the operator name typeof when referring to the mechanism for querying a type of an expression in general. The decltype operator refers to the proposed variant of typeof. ... Some compiler vendors (EDG, Metrowerks, GCC) provide a typeof operator as an extension with reference-dropping semantics. As described in Section 4, this appears to be ideal for expressing the type of variables. On the other hand, the reference-dropping semantics fails to provide a mechanism for exactly expressing the return types of generic functions ... In this proposal, the semantics of the operator that provides information of the type of expressions reflects the declared type. Therefore, we propose the operator to be named decltype."

    J. Jarvi, B. Stroustrup, D. Gregor, J. Siek: Decltype and auto. N1478/03-0061.

    So it's incorrect to say that decltype completely obviated typeof (if you want reference dropping semantics, then the typeof extension in those compilers still has use), but rather, typeof was largely obviated by it plus auto, which does drop references and replaces uses where typeof was used for variable inference.

    0 讨论(0)
  • 2021-01-01 11:20

    Well, typeof is a non-standard GNU C extension that you can use in GNU C++ because GCC allows you to use features from other languages in another (not always though), so they really shouldn't be compared.

    Of course other non-standard extensions may exist for other compilers, but GCC is definitely the most widely documented implementation.

    To answer the question: it can't be obsolete if it was never a feature.

    If you want to compare the merits of either method in C++ then there is semantically no difference unless you're dealing with references. You should use decltype because it is portable and standards conforming.

    0 讨论(0)
  • 2021-01-01 11:21

    There is no typeof operator in c++. While it is true that such a functionality has been offered by most compilers for quite some time, it has always been a compiler specific language extension. Therefore comparing the behaviour of the two in general doesn't make sense, since the behaviour of typeof (if it even exists) is extremely platform dependent.

    Since we now have a standard way of getting the type of a variable/expression, there is really no reason to rely on non portable extensions, so I would say it's pretty much obsolete.

    Another thing to consider is that if the behaviour is of typeof isn't compatible with decltype for a given compiler it is possible that the typeof extension won't get much development to encompass new language features in the future (meaning it might simply not work with e.g. lambdas). I don't know whether or not that is currently the case, but it is a distinct possibility.

    0 讨论(0)
  • 2021-01-01 11:24

    For legacy code, I have been using successfully the following:

    #include <type_traits>
    #define typeof(x) std::remove_reference<decltype((x))>::type
    
    0 讨论(0)
提交回复
热议问题