using f1 = voID(int);
我知道以下内容;
using f2 = voID(*)(int);using f3 = voID(&)(int);
f2是指向函数的指针,f3是参考.
解决方法 它是什么?这是一种功能类型.声明函数时,例如:
voID func(int);
它的类型不是指针也不是引用.上面函数的类型是voID(int).
我们可以通过使用类型特征来“证明”它如下:
voID func(int) {}int main() { std::cout << std::is_same<decltype(func),voID(int)>::value << '\n'; std::cout << std::is_same<decltype(func),voID(*)(int)>::value << '\n'; std::cout << std::is_same<decltype(func),voID(&)(int)>::value << '\n';}
Live demo
以上代码仅对第一行返回true.
它与指针或引用相同吗?
不,但函数左值可以隐式转换为函数指针,如下所示:
§4.3/1 Function-to-pointer conversion [conv.func]
An lvalue of function type T can be converted to a prvalue of type “pointer to T.” The result is a pointer to the function.
函数类型A(Args …)与其引用(即A(&)(Args …))之间的关系基本上与任何类型T与其引用(即T&)之间的关系相同.
它在哪里使用?
它通常用作模板参数.
例如,std :: function将函数类型存储在std :: function对象中,您可以使用以下命令声明这样的对象:
std::function<voID(int)> fn;总结
以上是内存溢出为你收集整理的c – 使用别名的功能全部内容,希望文章能够帮你解决c – 使用别名的功能所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)