tags:

views:

185

answers:

5

For instance:

template <typename Type1, typename Type2>
void fun(const Type1 &v1, const Type2 &v2)
{
    largest<Type1, Type2>::type val = v1 + v2;
    .
    .
    .
};

I'd like to know if there's a "largest" somewhere, perhaps in boost.

+8  A: 
template<bool, typename T1, typename T2>
struct is_cond {
    typedef T1 type;
};

template<typename T1, typename T2>
struct is_cond<false, T1, T2> {
    typedef T2 type;
};

template<typename T1, typename T2>
struct largest {
     typedef typename is_cond< (sizeof(T1)>sizeof(T2)), T1, T2>::type type;
};
Alexey Malistov
How will you compare unsigned int to int? There's both the same size.
wheaties
He specified 'in size' not in range, so I guess both the same size is the answer.
Alex Brown
@Neil Butterworth: OK, OK. Why repeat twice? I inserted `;` as you wish.
Alexey Malistov
@wheaties: To answer your question I need to know what type is larger in the OP's opinion. In the OP's question this is unkonown.
Alexey Malistov
@Neil Butterworth: it works for me. VS2008
Alexey Malistov
Still doesn't work IMHO. For example, largest <int,double> d; cout << sizeof(d) << endl; should print 8, I think. It prints 1.
anon
@Neil Butterworth: `largest <int,double>::type d; cout << sizeof(d) << endl;` prints 8. Attention! `largest <int,double>::type` rather than `largest <int,double>`
Alexey Malistov
You are right I apologise.
anon
I'm only interested int signed integral types, so this does it. Thanks Alexey!
Notice that the selected type is no necessarily identical to the result of the addition. For instance `char` + `short` = `int` on nowaday's machines, but you will select short (nothing bad - just want to note that)
Johannes Schaub - litb
Yes, it's actually for another use, this was just an example. But thanks for the remark.
This would be cleaner using `boost::mpl::if_` instead of a home grown `is_cond`.
caspin
Yes, boost::mpl::if_c is the one.
A: 

You probably could roll your own with sizeof.

http://www.cppreference.com/wiki/keywords/sizeof

Daniel A. White
@Xav: sizeof works well for the case of interger types.
Alexey Malistov
A: 

You can use the sizeof built-in function of c to get the memory size of the type. You can also call it on an instance of a type. For example:

return (sizeof(v1) > sizeof(v2));

Alex Brown
care to explain why the downvote?
Alex Brown
+2  A: 

There is no simple answer. If the largest type on your machine is a long and the two types passed are an unsigned long and a signed long, what type would you expect val to be? If unsigned you run the risk of a negative number which will not fit in it. If signed, you may overflow but still have a number that would fit in the unsigned number space.

If these limitations are acceptable you could use Alexey Malistov's approach, but the resulting type if Type1 and Type2 are the same size but different types will be different depending on the order the values are passed.

Take a look at the boost mpl function if_, with which you can pick one of two types. You'll need need to come up with your own rules for how to pick the resulting type.

Stephen Nutt
Even though he's only interested in specific types (signed integral) I'm glad you brought this up.
joshperry
You're right. The integrals should both be signed or unsigned. In my case I'm interested in signed ones, but your remark is correct.
+1  A: 

This won't be very feasible. How do you tell the difference between unsigned int and int? You can't use sizeof() because they're both the same "size" in the memory. I think you'll have to roll your own template specialization to handle those cases at which point I'd suggest just using an overloaded function.

wheaties
Since he asked for largest (in size), I don't think that's a problem.
Alex Brown
See http://en.wikipedia.org/wiki/Integer_(computer_science) See stephen's answer which is more in depth than mine.
wheaties
You're right. The integrals should both be signed or unsigned. In my case I'm interested in signed ones, but your remark is correct.