larger_type c++ template metaprogram

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

larger_type c++ template metaprogram

Postby EvanED » Wed Apr 16, 2008 5:52 pm UTC

This is inspired in part by the "Is there a language that does a < (b & c & d)?" thread. In that thread, as well as at other times, I have wanted the ability to have a template metafunction that functions so that larger_type<T, U> would return the "larger" of those two types. For instance:

Code: Select all

larger_type<short, char>  -> short
larger_type<int, float>   -> float


In general, I want something that might be written this way using C++0x extensions:

Code: Select all

template<typename LHS, typename RHS>
struct larger_type
{
    typedef  decltype(LHS() + RHS())  result;
};


The most recent legitimate motivation I've had is for a vector class. (In this case, vector as in a mathematical vector, not a CS, STL-like vector.) Consider the following code:

Code: Select all

T t;
U u;
auto t_plus_u = t + u;  // *

vec<T> vt;
vec<U> vu;
auto vt_plus_vu = vt + vu; // **
for any code where (*) compiles, I would like (**) to compile, and furthermore, if t_plus_u has type T_PLUS_U, I would like vt_plus_vu to have type VT_PLUS_VU.

(In thinking about this further, I think that rather than a single "larger_type", having separate templates for each operator I want to implement for vectors would be okay. Also it just occurred to me that larger_type may not even be what I want, because isn't a short + a short an int?)

I don't see anything in Boost::type_traits that helps this. Anyone know if there is a library with something along this line?

User avatar
evilbeanfiend
Posts: 2650
Joined: Tue Mar 13, 2007 7:05 am UTC
Location: the old world

Re: larger_type c++ template metaprogram

Postby evilbeanfiend » Wed Apr 16, 2008 10:05 pm UTC

well i suggested http://www.boost.org/doc/libs/1_35_0/li ... omote.html before which covers promoting smaller integers to bigger ones, but that wont cover promoting its to floats. however it shouldn't be too hard to do something similar which does do that i think.
in ur beanz makin u eveel

coppro
Posts: 117
Joined: Mon Feb 04, 2008 6:04 am UTC

Re: larger_type c++ template metaprogram

Postby coppro » Thu Apr 17, 2008 3:27 am UTC

std::numeric_limits should have enough information for you to be able to metaprogram around it.

User avatar
evilbeanfiend
Posts: 2650
Joined: Tue Mar 13, 2007 7:05 am UTC
Location: the old world

Re: larger_type c++ template metaprogram

Postby evilbeanfiend » Thu Apr 17, 2008 9:08 am UTC

yes don't you just need is_integral and is_floating_type and sizeof to differentiate between your types? assuming you always want to promote ints to floats if you are mixing the two?
in ur beanz makin u eveel

Konrad
Posts: 5
Joined: Thu Apr 17, 2008 6:28 pm UTC

Re: larger_type c++ template metaprogram

Postby Konrad » Thu Apr 17, 2008 6:36 pm UTC

Hi,

I created an account only to be able to reply to this. Unfortunately, this means I can't post a link here … but: Andrej Alexandrescu has written a very interesting article about just this problem. In a nutshell: the problem is unsolvable. Yes, Alexandrescu comes up with a perfect code but that has just a tiny drawback: it won't compile anywhere (at least, at the time he wrote the code it didn't).

Actually, Alexandrescu concludes that this limitation is one good reason to implement the standard functions 'min' and 'max' as macros rather than functions.

The article was published on Dr. Dobb's and is easily locatable via Google when searching for Alexandrescu and 'min' and 'max'. It's definitely worth looking at.

Hope that helps.

User avatar
wr3cktangle
Posts: 75
Joined: Tue Aug 01, 2006 5:03 pm UTC

Re: larger_type c++ template metaprogram

Postby wr3cktangle » Thu Apr 17, 2008 7:40 pm UTC

I believe this is the article Konrad referenced: http://www.ddj.com/cpp/184403774
Maxim 33. When faced with the unusual, self-destruct.
My blog

EvanED
Posts: 4331
Joined: Mon Aug 07, 2006 6:28 am UTC
Location: Madison, WI
Contact:

Re: larger_type c++ template metaprogram

Postby EvanED » Thu Apr 17, 2008 11:24 pm UTC

Konrad wrote:Hi,

I created an account only to be able to reply to this. Unfortunately, this means I can't post a link here … but: Andrej Alexandrescu has written a very interesting article about just this problem. .blah blah blah]
Hope that helps.

Awesome! Thank you very much. That gives me another lead to look for other solutions too. That article is from 2001, so I strongly suspect that modern compilers would be fine with it.

If I find or produce a solution, I'll let you know.

User avatar
froggie
Posts: 23
Joined: Wed Jul 18, 2007 6:50 pm UTC

Re: larger_type c++ template metaprogram

Postby froggie » Wed Apr 23, 2008 8:31 pm UTC

I've just read this and I'm not sure I understood your question, but isn't it something that you could implement by partial template specialization, i.e.

Code: Select all


template<typename T1, typename T2>
struct LargerType {};

template<>
struct LargerType<int, int> {
  typedef int larger_type;
};

template<>
struct LargerType<float, int> {
  typedef float larger_type;
};

template<>
struct LargerType<int, float> {
  typedef float larger_type;
};

//et caetera, ad nauseam

int main()
{
  //lt is a float
  LargerType<int, float>::larger_type lt = 4322.15;
  std::cout << lt << std::endl;
  return 0;
}



Return to “Coding”

Who is online

Users browsing this forum: No registered users and 9 guests