Const temporary from template type and why use std::add_const?

后端 未结 2 1433
一向
一向 2021-01-05 00:23

The following code is excerpted from cppreference.com.

#include 
#include 

struct foo
{
    void m() { std::cout <<          


        
2条回答
  •  日久生厌
    2021-01-05 00:48

    From what i remember it goes as follows, you can write 3 consts(3 is the number of purpose) in a function declaration.

    before the return type, after the function and its parameters, and on the parameters themselves.

    const at the end of a function signature means that the function should assume the object of which it is a member is const. In practical terms it means that you ask the compiler to check that the member function does not change the object data in any way. It means asking the compiler to check that it doesn't directly change any member data, and it doesn't call any function that itself does not guarantee that it won't change the object.

    before the return type means the thing the function is to return should be a const.

    const parameter means that the parameter cannot be changed.

    so the difference here is, the first call is not a const so it goes to the "non-cv", the second call is a const and so goes to "const".

    what i think of why VC++ goes to the same function both times is that call_m explicitly calls for T().m() thinking it shouldnt go to the const one.

提交回复
热议问题