Behavior of decltype

后端 未结 3 1492
感情败类
感情败类 2020-12-10 04:53

Say I have an object of some of stl container classes obj. I can define other object of same type this way:

decltype(obj) obj2;
<
3条回答
  •  难免孤独
    2020-12-10 05:13

    It's because of the way that the language is parsed.

    decltype(obj)::iterator it = obj.begin();
    

    You want it to become

    (decltype(obj)::iterator) it;
    

    But in actual fact, it becomes

    decltype(obj) (::iterator) it;
    

    I have to admit, I was also surprised to see that this was the case, as I'm certain that I've done this before. However, in this case, you could just use auto, or even decltype(obj.begin()), but in addition, you can do

    typedef decltype(obj) objtype;
    objtype::iterator it;
    

提交回复
热议问题