gpt4 book ai didi

c++ - 当类型参数为空时无法实例化函数模板

转载 作者:太空狗 更新时间:2023-10-29 20:05:39 25 4
gpt4 key购买 nike

<分区>

我们知道下面的代码compiles fine :

void f() {}
void g(void) {}

f();
g();

但为什么不是以下内容:

template<typename T>
void f() {}

template<typename T>
void g(T) {}

f<void>(); //ok
g<void>(); //error

为什么这行g<void>() gives error

error: no matching function for call to 'g()'

为什么不能用 void 实例化它作为类型参数?为什么函数模板没有实例化为:

void g(void) {}  //to be instantiated from the function template

毕竟是我自找的。

我正在寻找一个权威的答案。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com