C++标准中14.8.2第3和第4段的含义是什么?

Mao*_*Mao 7 c++ templates c++11

我正在努力理解这个规则,特别是下面的粗体句子(我的重点):

考虑注释#2在下面的代码片段:这是什么意思是说,功能类型f(int),但是tconst

§14.8.2/3:

执行此替换后,将执行8.3.5中描述的功能参数类型调整.[示例:参数类型" void ()(const int, int[5])"变为" void(*)(int,int*)".-end example] [注意:函数参数声明中的顶级限定符不会影响函数类型,但仍会影响函数中函数参数变量的类型.- 尾注 ] [示例:

template <class T> void f(T t);
template <class X> void g(const X x);
template <class Z> void h(Z, Z*);
int main() {
    // #1: function type is f(int), t is non const
    f<int>(1);
    // #2: function type is f(int), t is const
    f<const int>(1);
    // #3: function type is g(int), x is const
    g<int>(1);
    // #4: function type is g(int), x is const
    g<const int>(1);
    // #5: function type is h(int, const int*)
    h<const int>(1,0);
Run Code Online (Sandbox Code Playgroud)

}

- 末端的例子]

§14.8.2/4:

[注意:f<int>(1)并且f<const int>(1)调用不同的函数,即使调用的两个函数具有相同的函数类型. - 尾注]

eca*_*mur 7

考虑:

template <class T> void f(T t) { t = 5; }
Run Code Online (Sandbox Code Playgroud)

f<int>形式良好,但f<const int>不是,因为它试图分配给const变量.

请参阅:使用'const'作为函数参数


Bri*_*ian 6

如果你有两个功能

void f(int x);
void g(const int x);
Run Code Online (Sandbox Code Playgroud)

然后两个函数将具有相同的函数类型.表示此类型void(int),并且类型的函数指针void (*)(int)将能够指向任一函数.

当我们说const函数参数的顶级限定符不影响函数的类型时,这意味着什么.

但是,这并不意味着const没有意义.在定义中f,您将能够修改x,但在定义中g,您将无法修改x,因为x具有类型const int.