Usefulness of const (C++)

前端 未结 6 1035
眼角桃花
眼角桃花 2021-01-18 23:43

I\'m a const fiend, and I strive to make everything as const as possible.

I\'ve tried looking at various dissassembly outputs from const and non const versions of fu

6条回答
  •  遥遥无期
    2021-01-19 00:19

    As far as I know, the only effect of marking a function const is to allow the function to be called on a const object. There's no optimization benefit.

    Herb Sutter has an article which discusses const and optimization in depth:

    • http://www.gotw.ca/gotw/081.htm

    The one area that const is useful at the machine level is when applied to data - const data might be able to be placed in non-writable memory.

提交回复
热议问题