0

why both auto and decltype. cant auto only solve the purpose ?? what is output of this program Can someone give an example how auto and decltype is used in templates

 template <class A, class B>
auto findMin(A a, B b) -> decltype(a < b ? a : b)
{
    return (a < b) ? a : b;
}
  
// driver function to test various inference
int main()
{
    // This call returns 3.44 of doubale type
    cout << findMin(4, 3.44) << endl;
  
    // This call returns 3 of double type
    cout << findMin(5.4, 3) << endl;
  
    return 0;
}
  • 2
    Did you chance to read [decltype vs auto](https://stackoverflow.com/questions/12084040/)? – StoryTeller - Unslander Monica Jun 27 '21 at 11:59
  • 1
    Automatic return type detection was introduced in C++14. In your example, the trailing return type `-> decltype(a < b ? a : b)` is necessary in C++11, and is optional from C++14 onward. – Igor Tandetnik Jun 27 '21 at 12:04
  • it is returning double in both cases . why ,, and why not auto only – Rohan Uprety Jun 27 '21 at 12:11
  • 1
    `auto` is not a type, it is a declaration syntax element. `decltype(something)` is a type. If you need to write down a type of an expression, you cannot use `auto`, you can only use `decltype`. It is all explained at the [decltype vs auto](https://stackoverflow.com/questions/12084040/) question. – n. m. could be an AI Jun 27 '21 at 12:27
  • Because if you write the same expression in non-template code the result of the expression is `double`. It is not `int` sometimes and `double` otherwise. C++ does not work this way. The type of the given expression is always the same. – Sam Varshavchik Jun 27 '21 at 12:27

2 Answers2

1

Both auto and decltype let the compiler deduce a type without you explicitly writing what the type is, but there are some important differences between them:

auto defines the type of a variable according to its initializing value.

keep in mind that you don't always initialize values in the same line that you declare them in. Example :

int main() {
    auto a = 5;
    // auto b;  // compile error - can't use auto without an initializing value

    decltype(a) b;  // OK, can declare b to be the same type as a.
    b = 6; // then in the next line you can assign it an int
}

In template functions, auto alone is sufficient to let the compiler deduce the return type. However, you can also add decltype to auto to override the compiler deduced value. For Example:

template<class A, class B> auto add(A a, B b) -> decltype(b) {
    return a + b;
}

int main() {
    // sum will be double without the ->decltype(b) part in add, but int with it
    auto sum = add(5.0, 6); 
}
Gonen I
  • 5,576
  • 1
  • 29
  • 60
0

You can't declare a lambda expression without using auto or std::function.

Every lambda expression has a different type, because their types are generated when they are declared.

auto l = [] (int a, double b) {
    return a > b ? a : b;
};

decltype can not do that.


Also I found an interesting thing:

Given int a; double b;, and the type of a > b ? a : b is always double regardless of whether a is greater than b or not.

aleck099
  • 428
  • 2
  • 10
  • That is normal, a function needs a return type, and that type cannot be deduced at run time, so int gets promoted to double. – ChrisMM Jun 27 '21 at 13:11