Reputation: 57
I want to have a type in a template like T_signed in the code below
template <typename T_unsigned, typename T_signed> bool foo(T_unsigned input)
{
T_signed temp= ((T_signed) input)-100;
//use temp for calculations to figure out myBool
return myBool;
}
While the above is a simplification of the actual code I am writing and greatly believe it is what is preventing the code from compiling. How do I get the the compiler to figure out the type of T_signed implicitly based on what the type input is? Any help appreciated.
Upvotes: 1
Views: 92
Reputation: 2255
Something like this, using std::make_signed:
#include <iostream>
#include <type_traits>
template <typename Tu>
bool foo(Tu input) {
std::cout << std::is_signed<Tu>::value << std::endl;
typedef typename std::make_signed<Tu>::type Ts;
Ts temp = input - 100;
return (temp < 0);
}
int main() {
std::cout << foo(32u) << std::endl;
}
You can also add std::enable_if or static_assert to ensure that the passed in type really is unsigned.
Upvotes: 1