https://www.learncpp.com/cpp-tutorial/type-deduction-for-functions/
auto add(int x, int y)
{
return x + y;
}
이렇게 쓰면 deduce해서 에러가 나지 않는다
auto someFcn(bool b)
{
if (b)
return 5; // return type int
else
return 6.7; // return type double
}
이렇게 쓰면 분기마다 int, double로 결과가 달라지므로 컴파일러가 에러를 준다
auto foo();
int main()
{
std::cout << foo(); // the compiler has only seen a forward declaration at this point
return 0;
}
auto foo()
{
return 5;
}
이런식의 사용은 compile error를 발생한다
auto add(int x, int y) -> int
{
return (x + y);
}
위와 같이 쓸 수 있다
근데 그러면 왜 auto를 쓰냐고
아래와 같이 깔끔하게 볼 수 있기 때문이다
auto add(int x, int y) -> int;
auto divide(double x, double y) -> double;
auto printSomething() -> void;
auto generateSubstring(const std::string &s, int start, int len) -> std::string;
함수의 파라미터로 auto를 쓸 수 없다
void addAndPrint(auto x, auto y)
{
std::cout << x + y;
}
따라서 위와 같은 사용은 불가능하다