I think that the usage of the keyword auto
has become more efficient in the recent versions of C++. In older auto
without data type specifier
will default to int
so writing:
auto value = 5.7; // implicit conversion to int
std::cout << value; // 5
auto val2 = "Hello there!"; // error: cannot convert from char* to int
In new MSVC++ 14.0 I can:
auto val = "Hello"; // val is of type char*
std::cout << val; // Hello
auto dVal = 567.235; // dVal is of type double. Ok
Above is so good this eliminates the burden get a generic type.
But consider this example:
#include <iostream>
using namespace std;
void print(auto param) { // error here: A parameter cannot have type that contains 'auto'
cout << param << endl;
}
int main() {
print("Hello!"); // string
print(57); // int
print(3.14); // double
std::cout << std::endl;
std::cin.get();
return 0;
}
What I wanted above as long as the compiler chooses the right type for my variable according to the input value. I wanted to overcome function overloading make a function print
takes an auto
variable then when I call it the compiler will pass the relevant data type of the parameter.
But The problem I get compile-time error:
A parameter cannot have type that have 'auto'.
The thing is: This program works fine on gcc
and Ideone
compiler: https://ideone.com/i3DKzl