constant references with typedef and templates in c++

前端 未结 5 1435
伪装坚强ぢ
伪装坚强ぢ 2020-11-28 13:45

I heard the temporary objects can only be assigned to constant references.

But this code gives error

#include     
template

        
5条回答
  •  感动是毒
    2020-11-28 14:23

    It's a very common mistake for English speaking people, because of the way the English grammar works.

    I consider it extremely unfortunate that the C++ syntax would allow both:

    const int // immutable int
    int const // immutable int
    

    to have the same meaning.

    It doesn't make it easier, really, and isn't composable since:

    const int* // mutable pointer to immutable int
    int* const // immutable pointer to mutable int
    

    certainly do NOT have the same meaning.

    And this, unfortunately for you, what kicks in here, as @GMan explains.

    If you wish to avoid this kind of error in the future, take the habit of qualifying your types (const and volatile) on their right, then you'll be able to treat a typedef as simple text replacement.

提交回复
热议问题