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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章