C++11 学习笔记-04.trailing return type

函数声明

以后好好补充把,实在不爱看这一章

#include <iostream>
#include <string>
 
// 命名空间(文件)作用域中的声明
// (定义在后面提供)
int f1();
 
// 拥有默认实参的简单函数,不返回内容
void f0(const std::string& arg = "world")
{
    std::cout << "Hello, " << arg << '\n';
}
 
// 返回指向 f0 的指针的函数
auto fp11() -> void(*)(const std::string&)
{
    return f0;
}
 
// 返回指向 f0 的指针的函数,C++11 前的风格
void (*fp03())(const std::string&)
{
    return f0;
}
 
int main()
{
    f0();
    fp11()("test");
    fp03()("again");
    int f2(std::string); // 块作用域中的声明
    std::cout << f2("bad12") << '\n';
}
 
// 简单的非成员函数,返回 int
int f1()
{
    return 42;
}
 
// 拥有异常说明和函数 try 块的函数
int f2(std::string str) noexcept try
{ 
    return std::stoi(str);
}
catch(const std::exception& e)
{
    std::cerr << "stoi() failed!\n";
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章