How to infer the type of the function that can return type depending on condition?

Sonny

I have a template function that takes two data types - int and double and returns the one which is smaller, Now, How can I infer the type that will be returned by this function? Right now, I am losing the parts after the decimal points.

#include <iostream>

using namespace std;

template <class First, class Second>
First smaller(First a, Second b){
   return (a < b ? a : b);
}

int main () {
   int x = 100;
   double y = 15.5;
   cout<< smaller (x,y) << endl;

}
Persixty

The standard behaviour of the ternary operator is to perform 'usual arithmetic conversions'. The means promoting the int to a double in this case. That's the same as in auto x=1/1.0; is a double because of promotion of the integer numerator to be compatible with the double denominator.

See the answers to What is the type of "auto var = {condition} ? 1 : 1.0" in C++11? Is it double or int?. There are 'laymans terms' and standards quote answers. Both should help.

However what you've done is force the type to be `First':

template <class First, class Second>
First smaller(First a, Second b){
   return (a < b ? a : b);
}

Look at the return type you put in for smaller. So what is happening is it's being promoted to a double and then converted to an int return value.

Take a look at this:

#include <iostream>

template <class First, class Second>
First smaller(First a, Second b){
   return (a < b ? a : b);
}


template <class First, class Second>
auto smaller_auto(First a, Second b){
   return (a < b ? a : b);
}


int main() {
    int x=100;
    double y=15.5;

    std::cout<< smaller(x,y)<<std::endl; //First is int returns an int.

    std::cout<< smaller(y,x)<<std::endl;//First is double returns double.


    std::cout<< smaller_auto(x,y)<<std::endl; //Performs the usual arithemtic conversions (returns double).

    std::cout<< smaller_auto(y,x)<<std::endl;//Performs the usual arithemtic conversions (returns double).

    return 0;
}

Expected output:

15
15.5
15.5
15.5

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Infer return type of function based on type guard

From Dev

How to infer return type of Promise based on function arguments?

From Dev

TypeScript return exact function infer type

From Dev

Can C++ templates infer return type?

From Dev

Why TypeScript can not infer type of recursive function

From Dev

How to infer correctly a return type for a template?

From Dev

infer template argument type from function return type

From Dev

infer template argument type from function return type

From Java

Can a function return a type?

From Dev

How can I change the return type of this function?

From Dev

Infer function generic type U from return value of passed function

From Dev

Typescript function return type depending on number or type of arguments

From Dev

C++11 function wrapper type depending on wrappee return type

From Dev

How can I pass a type T to a method and have it infer the type?

From Dev

infer lambda return type in template

From Dev

condition in template function implementation depending on whether the type is a pointer

From Dev

Can typescript infer a function's response type based on parameter values?

From Dev

How can I write a function have a polymorphic return type based on the type argument of its type parameter?

From Dev

How can I type-hint a function where the return type depends on the input type of an argument?

From Dev

How to have different content type in ngRepeat depending on some condition?

From Java

Typescript return type depending on parameter

From Dev

Can not infer schema for type: <type 'str'>

From Dev

Swift: How can I make a function with a Subclass return type conform to a protocol, where a Superclass is defined as a return type?

From Dev

How can I define a return type of void for a function in a Typescript interface?

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

How can one type decorators to correctly infer types?

From Dev

Haskell - How to infer the type of action

From Java

Unable to infer complex closure return type with SwiftUI

Related Related

  1. 1

    Infer return type of function based on type guard

  2. 2

    How to infer return type of Promise based on function arguments?

  3. 3

    TypeScript return exact function infer type

  4. 4

    Can C++ templates infer return type?

  5. 5

    Why TypeScript can not infer type of recursive function

  6. 6

    How to infer correctly a return type for a template?

  7. 7

    infer template argument type from function return type

  8. 8

    infer template argument type from function return type

  9. 9

    Can a function return a type?

  10. 10

    How can I change the return type of this function?

  11. 11

    Infer function generic type U from return value of passed function

  12. 12

    Typescript function return type depending on number or type of arguments

  13. 13

    C++11 function wrapper type depending on wrappee return type

  14. 14

    How can I pass a type T to a method and have it infer the type?

  15. 15

    infer lambda return type in template

  16. 16

    condition in template function implementation depending on whether the type is a pointer

  17. 17

    Can typescript infer a function's response type based on parameter values?

  18. 18

    How can I write a function have a polymorphic return type based on the type argument of its type parameter?

  19. 19

    How can I type-hint a function where the return type depends on the input type of an argument?

  20. 20

    How to have different content type in ngRepeat depending on some condition?

  21. 21

    Typescript return type depending on parameter

  22. 22

    Can not infer schema for type: <type 'str'>

  23. 23

    Swift: How can I make a function with a Subclass return type conform to a protocol, where a Superclass is defined as a return type?

  24. 24

    How can I define a return type of void for a function in a Typescript interface?

  25. 25

    How can I define the return type of a lodash reduce function with Typescript?

  26. 26

    How can I define the return type of a lodash reduce function with Typescript?

  27. 27

    How can one type decorators to correctly infer types?

  28. 28

    Haskell - How to infer the type of action

  29. 29

    Unable to infer complex closure return type with SwiftUI

HotTag

Archive